<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="单据编码" prop="number">
        <el-input
          v-model="queryParams.number"
          placeholder="请输入单据编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="产品代码" prop="cfmaterialid">
        <el-input
          v-model="queryParams.cfmaterialid"
          placeholder="请输入产品代码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="产品名称" prop="cfmaterialname">
        <el-input
          v-model="queryParams.cfmaterialname"
          placeholder="请输入产品名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="来源单据编码" prop="sournumber" label-width="128px">
        <el-input
          v-model="queryParams.sournumber"
          placeholder="请输入来源单据编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-row></el-row>
      <el-form-item label="客户名称" prop="cfcustomerid">
        <el-input
          v-model="queryParams.cfcustomerid"
          placeholder="请输入客户名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="cfstate">
        <el-select v-model="queryParams.cfstate" placeholder="请选择单据状态" clearable>
          <el-option
            v-for="dict in dict.type.bill_state"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="制单人" prop="fcreatorid">
        <el-input
          v-model="queryParams.fcreatorid"
          placeholder="请输入制单人名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否进入产品库" prop="ffivouchered" label-width="128px">
        <el-select v-model="queryParams.ffivouchered" placeholder="请选择是否进入产品库" clearable>
          <el-option
            v-for="dict in dict.type.yes_no"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-row></el-row>
      <el-form-item label="制单日期">
        <el-date-picker
          v-model="daterangeFcreatetime"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-form-item label="是否查询分录" prop="isEntry" label-width="100px">
          <el-switch
            v-model="queryParams.isEntry"
            active-value="0"
            inactive-value="1"
            clearable
            @keyup.enter.native="handleQuery"
          ></el-switch>
        </el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['bomcostestimation/basebill:costcalc:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          size="mini"
          :disabled="single"
          @click="handleCopy"
          v-hasPermi="['bomcostestimation/basebill:costcalc:add']"
        >复制
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['bomcostestimation/basebill:costcalc:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['bomcostestimation/basebill:costcalc:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['bomcostestimation/basebill:costcalc:export']"
        >导出</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-dropdown class="avatar-container" trigger="click" :disabled="multiple">
          <div class="avatar-wrapper">
            <el-button
              type="primary"
              size="mini"
              plain
              :disabled="multiple"
              v-hasPermi="['bomcostestimation/basebill:costcalc:aduit']"
            >
              审核/反审核
              <i class="el-icon-bottom"/>
            </el-button>
          </div>
          <el-dropdown-menu slot="dropdown" class="user-dropdown" divided>
            <a>
              <el-dropdown-item lot="dropdown" class="user-dropdown" @click.native="handleAudit">
                审核
              </el-dropdown-item>
            </a>
            <a>
              <el-dropdown-item lot="dropdown" class="user-dropdown" divided @click.native="handleNoAudit">
                反审核
              </el-dropdown-item>
            </a>
          </el-dropdown-menu>
        </el-dropdown>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          size="mini"
          plain
          @click="handleSubmit"
          :disabled="multiple"
          v-hasPermi="['bomcostestimation/basebill:costcalc:submit']"
        >提交</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="info"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleCheckBelow"
          v-hasPermi="['bomcostestimation/basebill:calcdemand:checkBelow']"
        >上查</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading"
              :data="costcalcList"
              @selection-change="handleSelectionChange"
              border
              max-height="800px"
              stripe >
      <!--      :span-method="cellMerge"-->
      <el-table-column type="selection" width="55" align="center"  fixed />
      <el-table-column label="单据编码" align="center" prop="number" width="180" fixed>
        <template slot-scope="scope">
          <span
            onMouseOver="this.style.color='#034769'"
            onMouseOut="this.style.color='#3cade5'"
            @click="handleUpdate(scope.row)"
            style="
            color: #3cade5;
            cursor: pointer;
            text-decoration:underline;
          ">{{ scope.row.number }}</span>
        </template>
      </el-table-column>
      <el-table-column label="产品名称" align="center" prop="cfmaterialname"  width="240"/>
      <el-table-column label="客户" align="center" prop="cfcustomerid" width="250"/>
      <el-table-column label="制单人" align="center" prop="fcreatorid"/>
      <el-table-column label="制单日期" align="center" prop="fbizdate" width="120">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.fbizdate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="cfstate"  >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.bill_state" :value="scope.row.cfstate"/>
        </template>
      </el-table-column>
      <el-table-column label="是否进入产品库" align="center" prop="ffivouchered"  >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.yes_no" :value="scope.row.ffivouchered"/>
        </template>
      </el-table-column>
      <el-table-column label="产品代码" align="center" prop="cfmaterialid" width="150"/>
      <el-table-column label="是否有新品" align="center" prop="isnewmaterial" width="90" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.yes_no" :value="scope.row.isnewmaterial"/>
        </template>
      </el-table-column>
      <el-table-column label="是否存在上游单据" align="center" prop="istopbill"  >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.yes_no" :value="scope.row.istopbill"/>
        </template>
      </el-table-column>
      <el-table-column label="来源单据编码" align="center" prop="sournumber"  width="180" />
      <el-table-column label="合计材料成本" align="center" prop="cftotalmaterialcost"  />
      <el-table-column label="合计成本" align="center" prop="cftotalcost"  />
      <el-table-column label="含税成本" align="center" prop="cftaxcost"  />
      <el-table-column label="包装成本" align="center" prop="cfpackcost" />
      <el-table-column label="变动费用" align="center" prop="cfalterationfee"  />
      <el-table-column label="固定费用" align="center" prop="cffixedfee"  />
      <el-table-column label="税率" align="center" prop="cftaxrate" />
      <el-table-column label="规格" align="center" prop="cfmodel" />
      <el-table-column label="计量单位" align="center" prop="cfbaseunit" />
      <el-table-column label="项目" align="center" prop="cfprojecttxt" />
      <el-table-column label="备注" align="center" prop="cfremarktxt" width="240"/>
      <el-table-column v-if="this.getEntry" label="序号" align="left" prop="sub_index" width="200"/>
      <el-table-column v-if="this.getEntry" label="层级" align="center" prop="sub_level" width="50"/>
      <el-table-column v-if="this.getEntry" label="数据来源" prop="sub_cfbomcostcalcnumbe" width="150" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.bom_materialsourse" :value="scope.row.sub_cfbomcostcalcnumbe"/>
        </template>
      </el-table-column>
      <el-table-column v-if="this.getEntry" label="物料编码" prop="sub_cfmaterialhalfnumb" width="150" />
      <!--      <el-table-column v-if="true" label="长编码" prop="sub_cfbomcostcalcentryid" width="150" />-->
      <!--      <el-table-column v-if="true" label="父编码" prop="sub_cfrawmaterialnumbe" width="150" />-->
      <el-table-column v-if="this.getEntry" label="物料名称" prop="sub_cfmaterialhalfname" width="150"/>
      <el-table-column v-if="this.getEntry" label="单位" prop="sub_cfmaterialhalfunit" width="150"/>
      <el-table-column v-if="this.getEntry" label="用量" prop="sub_cfmaterialhalfdose" width="150"/>
      <el-table-column v-if="this.getEntry" label="税率" prop="sub_cftaxrate" width="150"/>
      <el-table-column v-if="this.getEntry" label="含税单价" prop="sub_cftaxprice" width="150"/>
      <el-table-column v-if="this.getEntry" label="去税金额" prop="sub_cfnotaxsum" width="150"/>
      <el-table-column v-if="this.getEntry" label="类别" prop="sub_cfcategoryname" width="150"/>
      <el-table-column v-if="this.getEntry" label="车间" prop="sub_cfworkshopname" width="150">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.bom_plant" :value="scope.row.sub_cfworkshopname"/>
        </template>
      </el-table-column>
      <el-table-column v-if="this.getEntry" label="变动费用小计" prop="sub_cfchangefee" width="150"/>
      <el-table-column v-if="this.getEntry" label="固定费用小计" prop="sub_cffixationfee" width="150"/>
      <el-table-column v-if="this.getEntry" label="人工变动(工序)" prop="sub_cfrgbd" width="150"/>
      <el-table-column v-if="this.getEntry" label="人工变动小计" prop="sub_cfrgbdxj" width="150"/>
      <el-table-column v-if="this.getEntry" label="人工固定(共耗)" prop="sub_cfrggd" width="150"/>
      <el-table-column v-if="this.getEntry" label="人工固定小计" prop="sub_cfrggdxj" width="150"/>
      <el-table-column v-if="this.getEntry" label="水变动(直接)" prop="sub_cfsbd" width="150"/>
      <el-table-column v-if="this.getEntry" label="水变动小计" prop="sub_cfsbdxj" width="150"/>
      <el-table-column v-if="this.getEntry" label="水固定(共耗)" prop="sub_cfsgd" width="150"/>
      <el-table-column v-if="this.getEntry" label="水固定小计" prop="sub_cfsgdxj" width="150"/>
      <el-table-column v-if="this.getEntry" label="电变动(直接)" prop="sub_cfdbd" width="150"/>
      <el-table-column v-if="this.getEntry" label="电变动小计" prop="sub_cfdbdxj" width="150"/>
      <el-table-column v-if="this.getEntry" label="电固定(共耗)" prop="sub_cfdgd" width="150"/>
      <el-table-column v-if="this.getEntry" label="电固定小计" prop="sub_cfdgdxj" width="150"/>
      <el-table-column v-if="this.getEntry" label="气变动(直接)" prop="sub_cfqbd" width="150"/>
      <el-table-column v-if="this.getEntry" label="气变动小计" prop="sub_cfqbdxj" width="150"/>
      <el-table-column v-if="this.getEntry" label="气固定(共耗)" prop="sub_cfqgd" width="150"/>
      <el-table-column v-if="this.getEntry" label="气固定小计" prop="sub_cfqgdxj" width="150"/>
      <el-table-column v-if="this.getEntry" label="折旧变动(专用)" prop="sub_cfzjbd" width="150"/>
      <el-table-column v-if="this.getEntry" label="折旧变动小计" prop="sub_cfzjbdxj" width="150"/>
      <el-table-column v-if="this.getEntry" label="折旧固定(共耗)" prop="sub_cfzjgd" width="150"/>
      <el-table-column v-if="this.getEntry" label="折旧固定小计" prop="sub_cfzjgdxj" width="150"/>
      <el-table-column v-if="this.getEntry" label="其他变动(共耗)" prop="sub_cfqtbd" width="150"/>
      <el-table-column v-if="this.getEntry" label="其他变动小计" prop="sub_cfqtbdxj" width="150"/>
      <el-table-column v-if="this.getEntry" label="其他固定(共耗)" prop="sub_cfqtgd" width="150"/>
      <el-table-column v-if="this.getEntry" label="其他固定小计" prop="sub_cfqtgdxj" width="150"/>
      <el-table-column v-if="this.getEntry" label="额定额" prop="sub_cfede" width="150"/>
      <el-table-column v-if="this.getEntry" label="额定额小计" prop="sub_cfedexj" width="150"/>
      <el-table-column label="操作" align="center" width="120" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['bomcostestimation/basebill:costcalc:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['bomcostestimation/basebill:costcalc:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <!-- 添加或修改BOM成本测算对话框 -->
    <el-dialog :close-on-click-modal="false" v-drag :title="title" :visible.sync="open" width="1500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-row>
          <el-col :span="8">
            <el-form-item label="单据编码" prop="number">
              <el-input v-model="form.number" placeholder="请输入单据编码" disabled/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="客户" prop="cfcustomerid">
              <el-input v-model="form.cfcustomerid" placeholder="请输入客户" readonly/>
              <span class="el-input__suffix">
                <span class="el-input__suffix-inner">
                  <i class="el-input_icon icon el-icon-search"
                     onmousemove="this.style.color='#366EDD'"
                     onmouseout="this.style.color='#919191'"
                     style="font-size: 16px;
                            position: absolute;
                            cursor: pointer;
                            right: 6px;
                            top: 8px;
                            background-color: #ffffff;"
                     @click="openF7('customer',false,null,false)"
                  />
                </span>
              </span>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="产品代码" prop="cfmaterialid">
              <el-input v-model="form.cfmaterialid" @change="removeBillMaterial" placeholder="请输入产品代码"/>
              <span class="el-input__suffix">
                <span class="el-input__suffix-inner">
                  <i class="el-input_icon icon el-icon-search"
                     onmousemove="this.style.color='#366EDD'"
                     onmouseout="this.style.color='#919191'"
                     style="font-size: 16px;
                            position: absolute;
                            cursor: pointer;
                            right: 6px;
                            top: 8px;
                            background-color: #ffffff;"
                     @click="openF7('specialmaterial',false,null,false)"
                  />
                </span>
              </span>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="产品名称" prop="cfmaterialname">
              <el-input v-model="form.cfmaterialname" :disabled="form.fhandlerid=='1'" placeholder="请输入产品名称" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="计量单位" prop="cfbaseunit">
              <el-input v-model="form.cfbaseunit" :disabled="form.fhandlerid=='1'" placeholder="请输入计量单位" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="制单日期" prop="fbizdate">
              <el-date-picker clearable
                              v-model="form.fbizdate"
                              type="date"
                              style="width: 366.67px"
                              value-format="yyyy-MM-dd"
                              placeholder="请选择业务日期">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="规格" prop="cfmodel">
              <el-input v-model="form.cfmodel" :disabled="form.fhandlerid=='1'" placeholder="请输入规格" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="税率" prop="cftaxrate">
              <el-input v-model="form.cftaxrate" :disabled="form.fhandlerid=='1'" placeholder="请输入税率"
                        onkeyup="this.value=this.value.match(/^[0-9]\d*$/);"
                        @change="formatBillAmount([])"/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="合计材料成本" prop="cftotalmaterialcost" >
              <el-input v-model="form.cftotalmaterialcost" placeholder="请输入合计材料成本" disabled readonly/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="变动费用" prop="cfalterationfee" >
              <el-input v-model="form.cfalterationfee" placeholder="请输入变动费用" disabled readonly/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="固定费用" prop="cffixedfee" >
              <el-input v-model="form.cffixedfee" placeholder="请输入固定费用" disabled readonly/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="包装成本" prop="cfpackcost">
              <el-input v-model="form.cfpackcost" placeholder="请输入包装成本" disabled/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="合计成本" prop="cftotalcost">
              <el-input v-model="form.cftotalcost" placeholder="请输入合计成本" disabled readonly/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="含税成本" prop="cftaxcost" >
              <el-input v-model="form.cftaxcost" placeholder="请输入含税成本" disabled readonly/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="状态" prop="cfstate" >
              <el-select v-model="form.cfstate" placeholder="请选择单据状态" disabled style="width: 366.67px" readonly clearable>
                <el-option
                  v-for="dict in dict.type.bill_state"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="时间类型" prop="cfseasontypeid">
              <el-select v-model="form.cfseasontypeid" placeholder="请选择时间类型" @change="changeSelection('seasontype')" style="width: 366.67px" clearable>
                <el-option
                  v-for="dict in dict.type.bom_seasontype"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="基地" prop="cfwfzbaseid">
              <el-select v-model="form.cfwfzbaseid" placeholder="请选择基地" @change="changeSelection('wfzbase')" style="width: 366.67px" clearable>
                <el-option
                  v-for="dict in dict.type.bom_wfzbase"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="是否进入产品库" prop="ffivouchered">
              <el-select v-model="form.ffivouchered" placeholder="请选择是否进入产品库" style="width: 366.67px" clearable>
                <el-option
                  v-for="dict in dict.type.yes_no"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="项目" prop="cfprojecttxt">
              <el-input v-model="form.cfprojecttxt" placeholder="请输入项目" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="提交部门" prop="deptName">
              <el-input v-model="form.deptName" placeholder="请输入提交部门" readonly/>
              <span class="el-input__suffix">
                <span class="el-input__suffix-inner">
                  <i class="el-input_icon icon el-icon-search"
                     onmousemove="this.style.color='#366EDD'"
                     onmouseout="this.style.color='#919191'"
                     style="font-size: 16px;
                            position: absolute;
                            cursor: pointer;
                            right: 6px;
                            top: 8px;
                            background-color: #ffffff;"
                     @click="openF7('costcenter',false,false,null)"
                  />
                </span>
            </span>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="品类" prop="fdescription">
              <el-select v-model="form.fdescription" placeholder="请选择品类" @change="changeSelection('usercategory')" style="width: 366.67px" clearable>
                <el-option
                  v-for="dict in dict.type.bom_usercategory"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="备注" prop="cfremarktxt">
              <el-input v-model="form.cfremarktxt" placeholder="请输入备注" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-divider content-position="center">{{entryParameter.entryName}}</el-divider>
        <el-row :gutter="10" class="mb8" v-show="entryParameter.entry1Show">
          <el-col :span="1.5">
            <el-button type="primary" icon="el-icon-plus" size="mini"
                       v-hasPermi="['bomcostestimation/basebill:costcalc:addEntry']"
                       @click="handleAddBomCostcalcentry">添加</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="primary" icon="el-icon-plus" size="mini"
                       v-hasPermi="['bomcostestimation/basebill:costcalc:addEntry']"
                       @click="handleAddBomCostcalcentryToNode">添加子级</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" icon="el-icon-delete" size="mini"
                       v-hasPermi="['bomcostestimation/basebill:costcalc:removeEntry']"
                       @click="handleDeleteBomCostcalcentry">删除</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="primary" size="mini"
                       v-hasPermi="['bomcostestimation/basebill:costcalc:introductionRawPrice']"
                       @click="introductionRawPrice">引入原材料价格</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="primary"
              plain
              icon="el-icon-sort"
              size="mini"
              @click="toggleExpandAll"
            >展开/折叠</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-dropdown class="avatar-container" trigger="click" :disabled="false">
              <div class="avatar-wrapper">
                <el-button
                  type="primary"
                  size="mini"
                  plain
                  :disabled="this.form.cfstate!='AUDIT'"
                >
                  移入/出产品库
                </el-button>
              </div>
              <el-dropdown-menu slot="dropdown" class="user-dropdown" divided>
                <a>
                  <el-dropdown-item lot="dropdown" class="user-dropdown" @click.native="moveProdect(true)">
                    移入产品库
                  </el-dropdown-item>
                </a>
                <a>
                  <el-dropdown-item lot="dropdown" class="user-dropdown" divided @click.native="moveProdect(false)">
                    移出产品库
                  </el-dropdown-item>
                </a>
              </el-dropdown-menu>
            </el-dropdown>
          </el-col>
          <el-col :span="1.5" style="float: right">
            <el-button type="primary" size="mini"
                       @click="showEntryAmount"
            >费用
            </el-button>
          </el-col>
          <el-col :span="1.5" style="float: right">
            <el-button type="primary" size="mini"
                       @click="showEntryMaterial"
                       style="border:none;background-color: #FF4D09"
            >原材料
            </el-button>
          </el-col>
        </el-row>
        <el-table :data="bomCostcalcentryList"
                  :row-class-name="rowBomCostcalcentryIndex"
                  @selection-change="handleBomCostcalcentrySelectionChange"
                  ref="bomCostcalcentry"
                  v-show="entryParameter.entry1Show"
                  :indent="10"
                  max-height="800px"
                  v-loading="entryloading"
                  v-if="refreshTable"
                  :default-expand-all="isExpandAll"
                  :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
                  row-key="cfbomcostcalcentryid"
                  border
        >
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column label="操作" align="center" type="" width="50" class-name="small-padding fixed-width">
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                icon="el-icon-delete"
                @click="handleDeleteBomCostcalcentry(scope.row)"
                v-hasPermi="['bomcostestimation/basebill:costcalc:removeEntry']"
              >删除</el-button>
            </template>
          </el-table-column>
          <el-table-column label="序号" align="left" prop="index" :width="indexColumnWidth"/>
          <el-table-column label="层级" align="center" prop="level" width="50"/>
          <el-table-column label="数据来源" prop="cfbomcostcalcnumbe" width="150">
            <template slot-scope="scope">
              <el-select v-model="scope.row.cfbomcostcalcnumbe" placeholder="请选择数据来源" @change="switchDataSource(scope.row)" clearable :disabled="isHisCbcs(scope.row)">
                <el-option
                  v-for="dict in dict.type.bom_materialsourse"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                />
              </el-select>
            </template>
          </el-table-column>
          <el-table-column label="物料编码" prop="cfmaterialhalfnumb" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.cfmaterialhalfnumb" placeholder="请输入物料BOM编码"
                        @change="materialNumberChange(scope.row)"/>
              <!--              v-hasPermi="['bomcostestimation/basebill:costcalc:addEntry1']"-->
              <!--              <el-input v-model="scope.row.cfmaterialhalfnumb" placeholder="请不要输入物料BOM编码" v-hasPermNo="['bomcostestimation/basebill:costcalc:addEntry1']"/>-->
              <span class="el-input__suffix"
              >
<!--                                  v-hasPermi="['bomcostestimation/basebill:costcalc:addEntry1']"-->
                <span class="el-input__suffix-inner">
                  <i class="el-input_icon icon el-icon-search"
                     onmousemove="this.style.color='#366EDD'"
                     onmouseout="this.style.color='#919191'"
                     style="font-size: 16px;
                            position: absolute;
                            cursor: pointer;
                            right: 6px;
                            top: 20px;
                            background-color: #ffffff;"
                     @click="openF7('material',true,scope.row)"
                  />
                </span>
              </span>
            </template>
          </el-table-column>
          <el-table-column label="物料名称" prop="cfmaterialhalfname" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.cfmaterialhalfname" placeholder="请输入物料BOM名称" :disabled="isHisCbcs(scope.row)" disabled/>
            </template>
          </el-table-column>
          <!--          <el-table-column label="父编码" prop="cfrawmaterialnumbe" width="450">-->
          <!--            <template slot-scope="scope">-->
          <!--              <el-input v-model="scope.row.cfrawmaterialnumbe" placeholder="请输入物料BOM编码" :disabled="isHisCbcs(scope.row)" readonly/>-->
          <!--            </template>-->
          <!--          </el-table-column>-->
          <!--          <el-table-column label="长编码" prop="cfbomcostcalcentryid" width="450">-->
          <!--            <template slot-scope="scope">-->
          <!--              <el-input v-model="scope.row.cfbomcostcalcentryid" placeholder="请输入物料BOM编码" :disabled="isHisCbcs(scope.row)"/>-->
          <!--            </template>-->
          <!--          </el-table-column>-->
          <!--          <el-table-column label="分类编码" prop="cfrawmaterialid" width="150">-->
          <!--            <template slot-scope="scope">-->
          <!--              <el-input v-model="scope.row.cfrawmaterialid" placeholder="请输入物料BOM编码" :disabled="isHisCbcs(scope.row)"/>-->
          <!--            </template>-->
          <!--          </el-table-column>-->
          <el-table-column label="单位" prop="cfmaterialhalfunit" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.cfmaterialhalfunit" placeholder="请输入物料BOM计量单位" :disabled="isHisCbcs(scope.row)" disabled/>
            </template>
          </el-table-column>
          <el-table-column label="用量" prop="cfmaterialhalfdose" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.cfmaterialhalfdose" placeholder="请输入物料BOM用量"
                        onkeyup="this.value=this.value.match(/\d+\.?\d{0,12}/);" :disabled="isHisCbcs(scope.row)" @change="formatEntry(scope.row)"/>
            </template>
          </el-table-column>
          <!--          <el-table-column label="初始用量" prop="cfdose" width="150">-->
          <!--            <template slot-scope="scope">-->
          <!--              <el-input v-model="scope.row.cfdose" placeholder="请输入初始用量" disabled/>-->
          <!--            </template>-->
          <!--          </el-table-column>-->
          <el-table-column label="税率" prop="cftaxrate" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.cftaxrate" placeholder="请输入税率" disabled/>
            </template>
          </el-table-column>
          <el-table-column label="含税单价" prop="cftaxprice" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.cftaxprice" placeholder="请输入含税价"
                        onkeyup="this.value=this.value.match(/\d+\.?\d{0,6}/);"
                        @change="formatEntry(scope.row,'prcie')"/>
            </template>
          </el-table-column>
          <!--          <el-table-column label="初始单价" prop="cfdeduction" width="150">-->
          <!--            <template slot-scope="scope">-->
          <!--              <el-input v-model="scope.row.cfdeduction" placeholder="请输入含税价"-->
          <!--                        disabled-->
          <!--                        onkeyup="this.value=this.value.match(/\d+\.?\d{0,6}/);"></el-input>-->
          <!--            </template>-->
          <!--          </el-table-column>-->
          <el-table-column label="去税金额" prop="cfnotaxsum" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.cfnotaxsum" placeholder="请输入去税金额" :disabled="isHisCbcs(scope.row)" disabled/>
            </template>
          </el-table-column>
        </el-table>
        <el-row :gutter="10" class="mb8" v-show="entryParameter.entry2Show">
          <el-col :span="1.5">
            <el-button type="primary" icon="el-icon-plus" size="mini" disabled
                       v-hasPermi="['bomcostestimation/basebill:costcalc:addEntry']"
                       @click="handleAddBomCostcalcentry">添加</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="primary" icon="el-icon-plus" size="mini" disabled
                       v-hasPermi="['bomcostestimation/basebill:costcalc:addEntry']"
                       @click="handleAddBomCostcalcentryToNode">添加子级</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" icon="el-icon-delete" size="mini" disabled
                       v-hasPermi="['bomcostestimation/basebill:costcalc:removeEntry']"
                       @click="handleDeleteBomCostcalcentry">删除</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="primary" size="mini"
                       v-hasPermi="['bomcostestimation/basebill:costcalc:introductionRawPrice']"
                       @click="introductionRawPrice">引入原材料价格</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="primary"
              plain
              icon="el-icon-sort"
              size="mini"
              @click="toggleExpandAll"
            >展开/折叠</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-dropdown class="avatar-container" trigger="click" :disabled="false">
              <div class="avatar-wrapper">
                <el-button
                  type="primary"
                  size="mini"
                  plain
                  :disabled="this.form.cfstate!='AUDIT'"
                >
                  移入/出产品库
                </el-button>
              </div>
              <el-dropdown-menu slot="dropdown" class="user-dropdown" divided>
                <a>
                  <el-dropdown-item lot="dropdown" class="user-dropdown" @click.native="moveProdect(true)">
                    移入产品库
                  </el-dropdown-item>
                </a>
                <a>
                  <el-dropdown-item lot="dropdown" class="user-dropdown" divided @click.native="moveProdect(false)">
                    移出产品库
                  </el-dropdown-item>
                </a>
              </el-dropdown-menu>
            </el-dropdown>
          </el-col>
          <el-col :span="1.5" style="float: right">
            <el-button type="primary" size="mini"
                       @click="showEntryAmount"
                       style="border:none;background-color: #ff4d09"
            >费用
            </el-button>
          </el-col>
          <el-col :span="1.5" style="float: right">
            <el-button type="primary" size="mini"
                       @click="showEntryMaterial"
            >原材料
            </el-button>
          </el-col>
        </el-row>
        <el-table :data="bomCostcalcentryList2"
                  :row-class-name="rowBomCostcalcentryIndex"
                  @selection-change="handleBomCostcalcentrySelectionChange"
                  ref="bomCostcalcentry"
                  v-show="entryParameter.entry2Show"
                  :indent="10"
                  max-height="800px"
                  :default-expand-all="isExpandAll"
                  :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
                  row-key="cfbomcostcalcentryid"
                  border
                  v-if="refreshTable"
        >
          <!--          v-if="refreshTable"-->
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column label="序号" align="left" prop="index" :width="indexColumnWidth"/>
          <el-table-column label="层级" align="center" prop="level" width="50"/>
          <!--          <el-table-column label="含税单价" prop="cftaxprice" width="150">-->
          <!--            <template slot-scope="scope">-->
          <!--              <el-input v-model="scope.row.cftaxprice" placeholder="请输入含税价"-->
          <!--                        disabled-->
          <!--                        onkeyup="this.value=this.value.match(/\d+\.?\d{0,6}/);"/>-->
          <!--            </template>-->
          <!--          </el-table-column>-->
          <el-table-column label="物料编码" prop="cfmaterialhalfnumb" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.cfmaterialhalfnumb" placeholder="请输入物料BOM编码" :disabled="isHisCbcs(scope.row)" readonly/>
            </template>
          </el-table-column>
          <el-table-column label="物料名称" prop="cfmaterialhalfname" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.cfmaterialhalfname" placeholder="请输入物料BOM名称" :disabled="isHisCbcs(scope.row)" disabled/>
            </template>
          </el-table-column>
          <el-table-column label="单位" prop="cfmaterialhalfunit" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.cfmaterialhalfunit" placeholder="请输入物料BOM计量单位" disabled/>
            </template>
          </el-table-column>
          <el-table-column label="用量" prop="cfmaterialhalfdose" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.cfmaterialhalfdose" placeholder="请输入物料BOM用量"
                        onkeyup="this.value=this.value.match(/\d+\.?\d{0,12}/);" :disabled="isHisCbcs(scope.row)" @change="formatEntry(scope.row)"/>
            </template>
          </el-table-column>
          <el-table-column label="类别" prop="cfcategoryname" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.cfcategoryname" placeholder="请输入类别" readonly :disabled="isHisCbcs(scope.row)" />
              <span class="el-input__suffix">
                <span class="el-input__suffix-inner">
                  <i class="el-input_icon icon el-icon-search"
                     onmousemove="this.style.color='#366EDD'"
                     onmouseout="this.style.color='#919191'"
                     style="font-size: 16px;
                            position: absolute;
                            cursor: pointer;
                            right: 6px;
                            top: 20px;
                            background-color: #ffffff;"
                     @click="openF7('category',true,scope.row)"
                  />
                </span>
              </span>
            </template>
          </el-table-column>
          <el-table-column label="车间" prop="cfworkshopname" width="150">
            <template slot-scope="scope">
              <el-select v-model="scope.row.cfworkshopname" placeholder="车间" :disabled="isHisCbcs(scope.row)" clearable>
                <el-option
                  v-for="dict in dict.type.bom_plant"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                />
              </el-select>
            </template>
          </el-table-column>
          <el-table-column label="变动费用小计" prop="cfchangefee" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.cfchangefee" placeholder="请输入变动费用小计" :disabled="isHisCbcs(scope.row)" />
            </template>
          </el-table-column>
          <el-table-column label="固定费用小计" prop="cffixationfee" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.cffixationfee" placeholder="请输入固定费用额" :disabled="isHisCbcs(scope.row)" />
            </template>
          </el-table-column>
          <el-table-column label="人工变动(工序)" prop="cfrgbd" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.cfrgbd" placeholder="请输入人工变动(工序)" disabled />
            </template>
          </el-table-column>
          <el-table-column label="人工变动小计" prop="cfrgbdxj" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.cfrgbdxj" placeholder="请输入人工变动小计" disabled />
            </template>
          </el-table-column>
          <el-table-column label="人工固定(共耗)" prop="cfrggd" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.cfrggd" placeholder="请输入人工固定(共耗)" disabled />
            </template>
          </el-table-column>
          <el-table-column label="人工固定小计" prop="cfrggdxj" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.cfrggdxj" placeholder="请输入人工固定小计" disabled />
            </template>
          </el-table-column>
          <el-table-column label="水变动(直接)" prop="cfsbd" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.cfsbd" placeholder="请输入水变动(直接)" disabled />
            </template>
          </el-table-column>
          <el-table-column label="水变动小计" prop="cfsbdxj" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.cfsbdxj" placeholder="请输入水变动小计" disabled />
            </template>
          </el-table-column>
          <el-table-column label="水固定(共耗)" prop="cfsgd" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.cfsgd" placeholder="请输入水固定(共耗)" disabled />
            </template>
          </el-table-column>
          <el-table-column label="水固定小计" prop="cfsgdxj" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.cfsgdxj" placeholder="请输入水固定小计" disabled />
            </template>
          </el-table-column>
          <el-table-column label="电变动(直接)" prop="cfdbd" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.cfdbd" placeholder="请输入电变动(直接)"  disabled/>
            </template>
          </el-table-column>
          <el-table-column label="电变动小计" prop="cfdbdxj" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.cfdbdxj" placeholder="请输入电变动小计" disabled />
            </template>
          </el-table-column>
          <el-table-column label="电固定(共耗)" prop="cfdgd" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.cfdgd" placeholder="请输入电固定(共耗)" disabled />
            </template>
          </el-table-column>
          <el-table-column label="电固定小计" prop="cfdgdxj" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.cfdgdxj" placeholder="请输入电固定小计" disabled />
            </template>
          </el-table-column>
          <el-table-column label="气变动(直接)" prop="cfqbd" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.cfqbd" placeholder="请输入气变动(直接)" disabled />
            </template>
          </el-table-column>
          <el-table-column label="气变动小计" prop="cfqbdxj" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.cfqbdxj" placeholder="请输入气变动小计" disabled />
            </template>
          </el-table-column>
          <el-table-column label="气固定(共耗)" prop="cfqgd" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.cfqgd" placeholder="请输入气固定(共耗)" disabled />
            </template>
          </el-table-column>
          <el-table-column label="气固定小计" prop="cfqgdxj" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.cfqgdxj" placeholder="请输入气固定小计" disabled />
            </template>
          </el-table-column>
          <el-table-column label="折旧变动(专用)" prop="cfzjbd" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.cfzjbd" placeholder="请输入折旧变动(专用)" disabled />
            </template>
          </el-table-column>
          <el-table-column label="折旧变动小计" prop="cfzjbdxj" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.cfzjbdxj" placeholder="请输入折旧变动小计" disabled />
            </template>
          </el-table-column>
          <el-table-column label="折旧固定(共耗)" prop="cfzjgd" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.cfzjgd" placeholder="请输入折旧固定(共耗)" disabled />
            </template>
          </el-table-column>
          <el-table-column label="折旧固定小计" prop="cfzjgdxj" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.cfzjgdxj" placeholder="请输入折旧固定小计" disabled />
            </template>
          </el-table-column>
          <el-table-column label="其他变动(共耗)" prop="cfqtbd" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.cfqtbd" placeholder="请输入其他变动(共耗)" disabled />
            </template>
          </el-table-column>
          <el-table-column label="其他变动小计" prop="cfqtbdxj" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.cfqtbdxj" placeholder="请输入其他变动小计" disabled />
            </template>
          </el-table-column>
          <el-table-column label="其他固定(共耗)" prop="cfqtgd" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.cfqtgd" placeholder="请输入其他固定(共耗)" disabled />
            </template>
          </el-table-column>
          <el-table-column label="其他固定小计" prop="cfqtgdxj" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.cfqtgdxj" placeholder="请输入其他固定小计" disabled />
            </template>
          </el-table-column>
          <el-table-column label="额定额" prop="cfede" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.cfede" placeholder="请输入额定额" disabled />
            </template>
          </el-table-column>
          <el-table-column label="额定额小计" prop="cfedexj" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.cfedexj" placeholder="请输入额定额小计" disabled />
            </template>
          </el-table-column>
        </el-table>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm" :disabled="this.form.cfstate=='AUDIT'">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <base-data-view @emitToParent="getF7Data" :tobasedata.sync='basedatadialog'></base-data-view>
  </div>
</template>

<script>
import { optionStatus, listCostcalc, listCostcalc2, getCostcalc, delCostcalc, addCostcalc, updateCostcalc,getMaterialBomAllNodes,reverse,getBomCoffe ,moveProdect } from "@/api/bomcostestimation/basebill/costcalc";
import baseDataView from "@/views/bomcostestimation/f7View/baseDataView";
import BOMSM from "@/views/bomcostestimation/basedocument/bomsm";
import BOMRM from "@/views/bomcostestimation/basedocument/rawmaterial";
import {listCoefficient} from "@/api/bomcostestimation/basedocument/coefficient";
import {listAsscategory} from "@/api/bomcostestimation/basedocument/asscategory";
import {introductionPrice} from "@/api/bomcostestimation/basedocument/rawmaterial";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import _ from 'lodash'
import { listBomsm,getListByNumber} from "@/api/bomcostestimation/basedocument/bomsm";
import { listMaterialhalf} from "@/api/bomcostestimation/basedocument/materialhalf";



export default {
  name: "Costcalc",
  dicts: ['bill_state','bom_wfzbase','bom_usercategory','bom_seasontype','bom_plant','bom_materialsourse',"yes_no"],
  components: {
    "baseDataView": baseDataView,
    BOMSM,
    BOMRM
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      entryloading: false,
      // 选中数组
      ids: [],
      numbers: [],
      states: [],
      cfcalcdemandid: [],
      // 子表选中数据
      checkedBomCostcalcentry: [],
      // 添加子级节点选择的数据
      checkedBomCostcalcentryToNode: [],
      // 重新渲染表格状态
      refreshTable: true,
      // 是否展开，默认全部展开
      isExpandAll: false,
      // 渲染的表格数据
      spanArr: [],
      // 渲染的表格数据
      spanArrTwo: [],
      getEntry: false,
      // 渲染的表格数据2
      pos: null,
      // 渲染的表格数据2
      posTwo: null,
      //序号宽度
      indexColumnWidth:50,
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // BOM成本测算表格数据
      costcalcList: [],
      // 成本测算分录表格数据
      bomCostcalcentryList: [],
      // 成本测算费用分录表格数据
      bomCostcalcentryList2: [],
      //分录展示数据
      entryParameter: {
        entryName: '原材料分录',
        entry1Show: true,
        entry2Show: false
      },
      // 弹出层标题
      // 是否显示弹出层
      open: false,
      isffivouchered: false,
      title: "",
      basedatadialog: {
        //定义一个参数用来存储当前需要修改的行数（存在分录的情况下）
        tempTableRowIndex: null,
        //定义一个参数用来判断当前是获取什么类型的数据
        F7dataType: null,
        wfzbase: {
          show: false
        },
        materialhalf: {
          show: false,
          index:null,
          row:null,
          isEntry:false,
          list:[],
          time: false,
          queryParams:{
            pageNum: 1,
            pageSize: 10,
            materialbomname: null,
            materialbomnumber: null,
            materialbomid: null,
          }
        },
        costcalclist: {
          show: false,
          index:null,
          row:null,
          isEntry:false,
          list:[],
          queryParams:{
            pageNum: 1,
            pageSize: 10,
            materialbomname: null,
            materialbomnumber: null,
            materialbomid: null,
          }
        },
        category: {
          show: false,
          index:null,
          row:null,
          isEntry:false,
          queryParams:{
            pageNum: 1,
            pageSize: 10,
            cfseasontypeid: null,
            fnumber: null,
            cfwfzbaseid: null,
            cfcategory: null,
            cfisnew: null,
          }
        },
        bomsm:{
          //是否显示特殊物料数据对话框
          isSMOpen:false,
          //选中的特殊物料信息
          selectedItem:null,
          //弹出的特殊物料是否用于查询
          isQuery:false,
          index:null,
          row:null,
        },
        specialmaterial: {
          show: false,
          index:null,
          row:null,
          isEntry:false,
          isSpecial:false,
        },
        customer: {
          show: false,
        },
        costcenter: {
          show: false,
        },
        calcdemand: {
          show: false,
          index: null,
          row: null,
          data: null,
          isEntry: true,
        },
      },
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        isEntry: "1",
        ids: null,
        fcreatorid: null,
        fcreatetime: null,
        flastupdateuserid: null,
        flastupdatetime: null,
        number: null,
        fbizdate: null,
        fhandlerid: null,
        longCode: null,
        fdescription: null,
        fhaseffected: null,
        fauditorid: null,
        fsourcebillid: null,
        fsourcefunction: null,
        ffivouchered: null,
        cfcustomerid: null,
        cftotalmaterialcost: null,
        cftotalcost: null,
        cftaxcost: null,
        cfpackcost: null,
        cfalterationfee: null,
        cffixedfee: null,
        cftaxrate: null,
        cfstate: null,
        cfmaterialid: null,
        cfmaterialname: null,
        cfmodel: null,
        cfbaseunit: null,
        cfprojecttxt: null,
        deptName: null,
        cfremarktxt: null,
        cfseasontypeid: null,
        cfwfzbaseid: null,
        sournumber: null,
      },
      // 制单时间范围
      daterangeFcreatetime: [],
      queryParamsToRawMaerial: {
        pageNum: 1,
        pageSize: 10,
        materialnumber: null,
        materialname: null,
        unitname: null,
        kingdeeid: null,
        status: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        deptName: [
          { required: true, message: "部门为空", trigger: "blur" }
        ],
        cfseasontypeid: [
          { required: true, message: "时间类型为空", trigger: "blur" }
        ],
        cfwfzbaseid: [
          { required: true, message: "基地不能为空", trigger: "blur" }
        ],
        fdescription: [
          { required: true, message: "品类不能为空", trigger: "blur" }
        ],
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询BOM成本测算列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeFcreatetime && '' != this.daterangeFcreatetime) {
        this.queryParams.params["beginFcreatetime"] = this.daterangeFcreatetime[0];
        this.queryParams.params["endFcreatetime"] = this.daterangeFcreatetime[1];
      }
      if(this.queryParams.isEntry==1){
        listCostcalc(this.queryParams).then(response => {
          this.getEntry = false;
          this.costcalcList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      }else if(this.queryParams.isEntry==0){
        // listCostcalc(this.queryParams).then(response => {
        // this.total = response.total;
        listCostcalc2(this.queryParams).then(response => {
          this.getEntry = true;
          let costcalcList = response.rows;
          this.total = response.total;
          this.costcalcList = costcalcList;
          this.loading = false;
        });
        // });
      }
    },
    sortedNumbers(list) {
      // 复制原始数组以避免修改原数组
      const sortedArray = structuredClone(list);
      // 自定义排序逻辑
      sortedArray.sort((a, b) => {
        const aParts = a.index.split('.').map(Number);
        const bParts = b.index.split('.').map(Number);
        for (let i = 0; i < Math.max(aParts.length, bParts.length); i++) {
          if (aParts[i] === undefined) return 1; // a 较短，排在前面
          if (bParts[i] === undefined) return -1;  // b 较短，排在后面
          if (aParts[i] !== bParts[i]) return aParts[i] - bParts[i];
        }
        return 0; // 完全相等
      });
      return sortedArray;
    },
    cellMerge({row, column, rowIndex, columnIndex}) {
      let arr = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21]
      if (arr.includes(columnIndex)) {
        const _row = this.spanArr[rowIndex];
        const _col = _row > 0 ? 1 : 0;
        return {
          rowspan: _row,
          colspan: _col
        }
      }
    },
    getSpanArr(data) {
      this.spanArr = [];
      this.spanArrTwo = [];
      for (var i = 0; i < data.length; i++) {
        if (i === 0) {
          this.spanArr.push(1);
          this.pos = 0
        } else {
          // 判断当前元素与上一个元素是否相同
          if (data[i].number === data[i - 1].number) {
            this.spanArr[this.pos] += 1;
            this.spanArr.push(0);
          } else {
            this.spanArr.push(1);
            this.pos = i;
          }
        }
      }
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        fid: null,
        fcreatorid: this.$store.state.user.name,
        fcreatetime: null,
        flastupdateuserid: null,
        flastupdatetime: null,
        number: null,
        fbizdate: new Date(),
        fhandlerid: null,
        longCode: null,
        fdescription: null,
        fhaseffected: null,
        fauditorid: null,
        fsourcebillid: null,
        fsourcefunction: null,
        ffivouchered: "0",
        cfcustomerid: null,
        cftotalmaterialcost: null,
        cftotalcost: null,
        cftaxcost: null,
        cfpackcost: null,
        cfalterationfee: null,
        cffixedfee: null,
        cftaxrate: null,
        cfstate: null,
        cfmaterialid: null,
        cfmaterialname: null,
        cfmodel: null,
        cfbaseunit: null,
        cfprojecttxt: null,
        deptName: null,
        cfremarktxt: null,
        cfseasontypeid: null,
        cfwfzbaseid: null
      };
      this.bomCostcalcentryList = [];
      this.bomCostcalcentryList2 = [];
      this.handleAddBomCostcalcentry();
      this.showEntryMaterial();
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeFcreatetime = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.fid)
      this.numbers = selection.map(item => item.number)
      this.cfcalcdemandid = selection.map(item => item.cfcalcdemandid)
      this.single = selection.length!==1
      this.multiple = !selection.length
      this.states = selection.map(item => item.cfstate)
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      if(this.$store.state.user.dept.deptName){
        this.form.deptName = this.$store.state.user.dept.deptName;
        this.form.fsourcefunction = this.$store.state.user.dept.deptNumber;
      }
      this.open = true;
      this.entryloading = false;
      this.indexColumnWidth = 50
      this.title = "添加BOM成本测算";
      this.form.cfstate="SAVE";
    },
    /** 复制按钮操作 */
    handleCopy(row) {
      this.reset();
      const id = this.ids
      getCostcalc(id).then(response => {
        this.form = response.data;
        this.form.fid = null;
        this.form.number = null;
        this.form.cfstate = "SAVE";
        this.form.fbizdate = new Date();
        this.form.fcreatetime = new Date();
        this.form.istopbill = "";
        this.form.cfcalcdemandid = "";
        this.form.sournumber = "";
        let isffivouchered = this.form.ffivouchered!="1";
        this.isffivouchered = isffivouchered;
        if(this.form.ffivouchered!=null){
          this.form.ffivouchered = this.form.ffivouchered+"";
        }
        this.bomCostcalcentryList = response.data.bomCostcalcentryList;
        this.formatBillAmount(this.bomCostcalcentryList)
        this.open = true;
        this.entryloading = false;
        this.title = "修改BOM成本测算";
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const fid = row.fid || this.ids
      getCostcalc(fid).then(response => {
        this.form = response.data;
        let isffivouchered = this.form.ffivouchered!="1";
        this.isffivouchered = isffivouchered;
        if(this.form.ffivouchered!=null){
          this.form.ffivouchered = this.form.ffivouchered+"";
        }
        this.bomCostcalcentryList = response.data.bomCostcalcentryList;
        this.formatBillAmount(this.bomCostcalcentryList)
        this.open = true;
        this.entryloading = false;
        this.title = "修改BOM成本测算";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.entryloading = true;
          this.form.bomCostcalcentryList = this.getTreeData(this.bomCostcalcentryList);
          if (this.form.fid != null) {
            updateCostcalc(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
              this.entryloading = false;
            }).catch(() => {
              this.entryloading = false;
            });
          } else {
            addCostcalc(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
              this.entryloading = false;
            }).catch(() => {
              this.entryloading = false;
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      let id = row.fid || this.ids;
      let csxqids = row.cfcalcdemandid || this.cfcalcdemandid;
      const ids = [];
      let billnos = row.number || this.numbers;
      const fstate = "AUDIT"; //审核状态
      let numbers = [];
      let state = row.cfstate || this.states;
      if (state == null || state.length === 0) {
        this.$modal.msgError("请选择单据");
        return;
      }
      if(!Array.isArray(id)){
        id = [id];
      }
      if(!Array.isArray(billnos)){
        billnos = [billnos];
      }
      if(!Array.isArray(state)){
        state = [state];
      }
      if(!Array.isArray(csxqids)){
        csxqids = [csxqids];
      }
      for (let i = 0; i < state.length; i++) {
        if (state[i] === "SAVE") {
          ids.push(id[i])
        }else {
          numbers.push(billnos[i])
        }
      }
      if (numbers.length === id.length) {
        this.$modal.msgError("选中的单据不符合操作");
        return;
      }
      this.$modal.confirm('是否确认删除BOM成本测算编号为"' + billnos + '"的数据项？').then(function() {
        return delCostcalc(ids);
      }).then(() => {
        //反写
        reverse(csxqids);
      }).then(() => {
        this.getList();
        if (numbers.length === 0) {
          this.$modal.msgSuccess("删除成功");
        } else {
          this.$modal.msgError("存在单据:" + numbers + ",不是暂存状态,无法删除,其余单据已删除");
        }
      }).catch(() => {});
    },
    /** 成本测算分录序号 */
    rowBomCostcalcentryIndex({ row, rowIndex }) {
      // row.index = rowIndex + 1;
    },
    /** 成本测算分录添加按钮操作 */
    handleAddBomCostcalcentry() {
      let obj = this.creatNewEntry();
      this.bomCostcalcentryList.push(obj);
    },
    /** 成本测算分录添加子级按钮操作 */
    handleAddBomCostcalcentryToNode() {
      // this.checkedBomCostcalcentryToNode
      if(this.checkedBomCostcalcentryToNode.length!=1){
        this.$modal.msgError("请选择一个成本测算分录");
        return;
      }
      if (!this.checkedBomCostcalcentryToNode[0].children) {
        this.checkedBomCostcalcentryToNode[0].children = [];
      }
      let obj = this.creatNewEntry();
      obj.cfbomcostcalcnumbe = this.checkedBomCostcalcentryToNode[0].cfbomcostcalcnumbe;
      obj.cfisbomcostcalcdetail = 4;
      obj.cfrawmaterialnumbe = this.checkedBomCostcalcentryToNode[0].cfbomcostcalcentryid;
      obj.cfbomcostcalcentryid = this.checkedBomCostcalcentryToNode[0].cfbomcostcalcentryid+","+obj.cfbomcostcalcentryid;
      this.checkedBomCostcalcentryToNode[0].children.push(obj);
      let bomCostcalcentryList = this.getTreeData(this.bomCostcalcentryList)
      this.bomCostcalcentryList = this.setTreeDataIncludeOrdinalwidth(bomCostcalcentryList)
    },
    creatNewEntry(){
      let obj = {};
      obj.fparentid = "";
      obj.cflossrate = "";
      obj.cftaxprice = "";
      obj.cftaxrate = "";
      obj.cfnotaxsum = "";
      obj.cfdeduction = "";
      obj.cfpackcost = "";
      obj.cfalterationfee = "";
      obj.cffixedfee = "";
      obj.cfmaterialhalfnumb = "";
      obj.cfmaterialhalfname = "";
      obj.cfmaterialhalshow = "";
      obj.cfmaterialhalfcode = "";
      obj.cfmaterialhalfunit = "";
      obj.cfmaterialhalfdose = "";
      obj.cfrawmaterialid="";
      obj.cfrawmaterialnumbe = "";
      obj.level = "";
      obj.cfrawmaterialunit = "";
      obj.cfcategoryid = "";
      obj.cfcategoryname = "";
      obj.cfworkshopname = "";
      obj.cfchangefee = "";
      obj.cffixationfee = "";
      obj.cfspecialmaterialid="";
      obj.cfspecialmaterialn = "";
      obj.cfspecialmaterialname = "";
      obj.cfspecialmaterialunit = "";
      obj.cfdose = "";
      obj.cfrawmaterialconsumption = "";
      obj.cfbomcostcalcnumbe = "";
      obj.cfbomcostcalcqty = "";
      obj.cfisbomcostcalcdetail = "";
      obj.cfbomcostcalcrowindex = "";
      obj.cfbomcostcalcentryid = new Date().getTime()+"1";
      obj.cfrgbd = "";
      obj.cfrggd = "";
      obj.cfsbd = "";
      obj.cfsgd = "";
      obj.cfdbd = "";
      obj.cfdgd = "";
      obj.cfqbd = "";
      obj.cfqgd = "";
      obj.cfzjbd = "";
      obj.cfzjgd = "";
      obj.cfqtbd = "";
      obj.cfqtgd = "";
      obj.cfede = "";
      obj.cfrgbdxj = "";
      obj.cfrggdxj = "";
      obj.cfsbdxj = "";
      obj.cfsgdxj = "";
      obj.cfdbdxj = "";
      obj.cfdgdxj = "";
      obj.cfqbdxj = "";
      obj.cfqgdxj = "";
      obj.cfzjbdxj = "";
      obj.cfzjgdxj = "";
      obj.cfqtbdxj = "";
      obj.cfqtgdxj = "";
      obj.cfedexj = "";
      obj.children = [];
      obj.index = this.bomCostcalcentryList.length-0+1+"";
      obj.level = "1";
      return obj;
    },
    //展示分录5
    showEntryAmount() {
      this.entryParameter.entryName = '费用分录';
      this.entryParameter.entry2Show = true;
      this.entryParameter.entry1Show = false;
      this.bomCostcalcentryList2 = this.filterList(this.bomCostcalcentryList);
    },
    //展示分录6
    showEntryMaterial() {
      this.entryParameter.entryName = '原材料分录';
      this.entryParameter.entry1Show = true;
      this.entryParameter.entry2Show = false;
    },
    /** 成本测算分录删除按钮操作 */
    handleDeleteBomCostcalcentry(row) {
      if(row!=undefined&&row!=null&&row.index!=null){
        let checkedBomCostcalcentry = [];
        checkedBomCostcalcentry = checkedBomCostcalcentry.concat(this.getIndices(row));
        let bomCostcalcentryList = this.bomCostcalcentryList;
        bomCostcalcentryList = this.getTreeData(bomCostcalcentryList);
        let bomCostcalcentryList1 = bomCostcalcentryList.filter(function(item) {
          return checkedBomCostcalcentry.indexOf(item.index) == -1
        });
        this.bomCostcalcentryList = this.handleTree(bomCostcalcentryList1, "cfbomcostcalcentryid", "cfrawmaterialnumbe");
        this.formatBillAmount([])
      }else{
        if (this.checkedBomCostcalcentry.length == 0) {
          this.$modal.msgError("请先选择要删除的成本测算分录数据");
        } else {
          let bomCostcalcentryList = this.bomCostcalcentryList;
          const checkedBomCostcalcentry = this.checkedBomCostcalcentry;
          bomCostcalcentryList = this.getTreeData(bomCostcalcentryList);
          let bomCostcalcentryList1 = bomCostcalcentryList.filter(function(item) {
            return checkedBomCostcalcentry.indexOf(item.index) == -1
          });
          this.bomCostcalcentryList = this.handleTree(bomCostcalcentryList1, "cfbomcostcalcentryid", "cfrawmaterialnumbe");
          this.formatBillAmount([])
        }
      }
    },
    /** 复选框选中数据 */
    handleBomCostcalcentrySelectionChange(selection) {
      // this.checkedBomCostcalcentry = selection.map(item => item.index)
      this.checkedBomCostcalcentryToNode = selection.map(item => item)
      let checkedBomCostcalcentry = [];
      for (let item of selection) {
        checkedBomCostcalcentry = checkedBomCostcalcentry.concat(this.getIndices(item));
      }
      this.checkedBomCostcalcentry = checkedBomCostcalcentry
    },
    getIndices(node) {
      let indices = [node.index]; // 当前节点的序号
      if (node.children && node.children.length > 0) {
        for (let child of node.children) {
          indices = indices.concat(this.getIndices(child)); // 递归获取子节点的序号
        }
      }
      return indices; // 返回当前节点及其所有子节点的序号
    },
    /** 导出按钮操作 */
    handleExport() {
      let ids = this.ids;
      this.queryParams.ids = ids;
      this.download('bomcostestimation/basebill/costcalc/export', {
        ...this.queryParams
      }, `costcalc_${new Date().getTime()}.xlsx`,{timeout: 1800000})
    },
    isHisCbcs(row){
      if(row.cfisbomcostcalcdetail==2){
        return false;
      }else{
        return false;
      }
    },
    switchDataSource(row){
      let creatNewEntry1 = this.clearEntry(row);
      creatNewEntry1.children = [];
      let bomCostcalcentryList = this.bomCostcalcentryList;
      let treeData = this.getTreeData(bomCostcalcentryList);
      let list = [];
      for (let i = 0; i < treeData.length; i++) {
        if(treeData[i].cfbomcostcalcentryid == creatNewEntry1.cfbomcostcalcentryid){
          // treeData[i] = creatNewEntry1;
          list.push(creatNewEntry1)
        }else {
          if(!(treeData[i].cfbomcostcalcentryid+"").includes(creatNewEntry1.cfbomcostcalcentryid+"")){
            list.push(treeData[i])
          }
        }
      }
      this.bomCostcalcentryList = this.setTreeDataIncludeOrdinalwidth(list)
    },
    clearEntry(row){
      let creatNewEntry1 = this.creatNewEntry();
      creatNewEntry1.cfbomcostcalcentryid = row.cfbomcostcalcentryid;
      creatNewEntry1.cfrawmaterialnumbe = row.cfrawmaterialnumbe;
      creatNewEntry1.cfbomcostcalcnumbe = row.cfbomcostcalcnumbe;
      creatNewEntry1.children = row.children;
      return creatNewEntry1;
    },
    /** 单据审核*/
    handleAudit(row) {
      const id = row.fid || this.ids;
      const ids = [];
      const billnos = row.number || this.numbers;
      const fstate = "AUDIT"; //审核状态
      let numbers = [];
      let state = row.cfstate || this.states;
      if (state == null || state.length === 0) {
        this.$modal.msgError("请选择单据");
        return;
      }
      for (let i = 0; i < state.length; i++) {
        if (state[i] === "SUBMIT") {
          ids.push(id[i])
        } else {
          numbers.push(billnos[i])
        }
      }
      if (numbers.length === id.length) {
        this.$modal.msgError("选中的单据不符合操作");
        return;
      }
      optionStatus(ids, fstate).then(response => {
        this.getList(); //刷新状态
        if (numbers.length === 0) {
          this.$modal.msgSuccess("已执行审核操作");
        } else {
          this.$modal.msgError("存在单据:" + numbers + ",不是暂存状态,无法审核,其余单据已审核");
        }
      });
    },
    /** 单据提交*/
    handleSubmit(row) {
      const id = row.fid || this.ids;
      const ids = [];
      const billnos = row.number || this.numbers;
      const fstate = "SUBMIT"; //提交
      let numbers = [];
      let numbersBySubmit = [];
      let state = row.cfstate || this.states;
      if (state == null || state.length === 0) {
        this.$modal.msgError("请选择单据");
        return;
      }
      for (let i = 0; i < state.length; i++) {
        if (state[i] === "SAVE") {
          ids.push(id[i]);
          numbersBySubmit.push(billnos[i]);
        } else {
          numbers.push(billnos[i])
        }
      }
      if (numbers.length === id.length) {
        this.$modal.msgError("选中的单据都不是暂存状态，不允许提交");
        return;
      }
      optionStatus(ids, fstate).then(response => {
        this.getList(); //刷新状态
        if (numbers.length === 0) {
          let data = response.data;
          if(data.length==0){
            this.$modal.msgSuccess("已执行提交操作");
          }else if(data.length==ids.length){
            this.$modal.msgError("已执行提交操作,但全部提交失败,失败原因可能是分录，类别为空或存在含税单价为0的数据");
          }else{
            this.$modal.msgError("已执行提交操作,存在部分提交失败的单据为："+data+",失败原因可能是分录，类别为空或存在含税单价为0的数据");
          }
        } else {
          let data = response.data;
          if(data.length==0){
            this.$modal.msgError("存在单据:" + numbers + ",不是暂存状态,无法提交,其余单据已提交");
          }else if(data.length==ids.length){
            this.$modal.msgError("存在单据:" + numbers + ",不是暂存状态,无法提交,其余单据已执行提交操作,但全部提交失败,失败原因可能是分录，类别为空或存在含税单价为0的数据");
          }else{
            this.$modal.msgError("存在单据:" + numbers + ",不是暂存状态,无法提交,其余单据已执行提交操作,存在部分提交失败的单据为："+data+",失败原因可能是分录，类别为空或存在含税单价为0的数据");
          }
        }
      });
    },
    /** 单据反审核*/
    handleNoAudit(row) {
      const id = row.fid || this.ids;
      const ids = [];
      const billnos = row.number || this.numbers;
      const fstate = "SAVE"; //暂存状态
      var numbers = [];
      var state = this.states;
      if (state == null || state.length === 0) {
        this.$modal.msgError("请选择单据");
        return;
      }
      for (var i = 0; i < state.length; i++) {
        if (state[i] === "AUDIT") {
          ids.push(id[i])
        } else {
          numbers.push(billnos[i])
        }
      }
      if (numbers.length === id.length) {
        this.$modal.msgError("选中的单据不符合操作");
        return;
      }
      optionStatus(ids, fstate).then(response => {
        this.getList(); //刷新状态
        if (numbers.length === 0) {
          this.$modal.msgSuccess("已执行反审核操作");
        } else {
          this.$modal.msgError("存在单据:" + numbers + ",不是审核状态,无法反审核,其余单据已反审核");
        }
      });
    },
    moveProdect(isMove){
      moveProdect(this.form.fid,isMove).then( response =>{
        if(isMove){
          this.form.ffivouchered = "1"
        }else{
          this.form.ffivouchered = "0"
        }
        this.$modal.msgSuccess("操作成功");
      })
    },
    /** 过滤空节点 */
    filterList(source) {
      const filterRecursive = (items) => {
        if (!Array.isArray(items)) {
          return []; // 如果 items 不是数组，返回空数组
        }
        return items
          .filter(item => (Array.isArray(item.children) && item.children.length > 0)
            //顶级节点为物料的就进费用
            // || (item.cfbomcostcalcnumbe=="2"&&!item.cfbomcostcalcentryid.includes(","))
            //顶级节点为物料且分类编码开头是50的进
            || (item.cfbomcostcalcnumbe=="2"&&!item.cfbomcostcalcentryid.includes(",")&&item.cfrawmaterialid!=undefined
              &&item.cfrawmaterialid!=null&&item.cfrawmaterialid.length!=0&&item.cfrawmaterialid.substring(0, 2)=="50")
          )
          .map(item => ({
            ...item,
            children: filterRecursive(item.children) // 递归过滤子节点
          }));
      };
      return filterRecursive(source);
    },
    /** 获取叶子节点 */
    getLeaves(source) {
      const getLeavesRecursive = (items) => {
        if (!Array.isArray(items)) {
          return []; // 如果 items 不是数组，返回空数组
        }
        let leaves = [];
        items.forEach(item => {
          if (Array.isArray(item.children) && item.children.length > 0) {
            // 如果有子节点，递归处理子节点
            leaves = leaves.concat(getLeavesRecursive(item.children));
          } else {
            // 如果没有子节点，添加到叶子节点列表
            leaves.push(item);
          }
        });
        return leaves;
      };
      return getLeavesRecursive(source);
    },
    /** 引入原材料价格*/
    introductionRawPrice(){
      this.entryloading = true;
      let bomCostcalcentryList = this.getLeaves(this.bomCostcalcentryList);
      // let bomCostcalcentryList = this.getTreeData(this.bomCostcalcentryList);
      let numbers = [];
      let longCode = [];
      for (let i = 0; i < bomCostcalcentryList.length; i++) {
        let Element = bomCostcalcentryList[i];
        let cfmaterialhalfnumb = Element.cfmaterialhalfnumb;
        numbers.push(cfmaterialhalfnumb);
        longCode.push(Element.cfbomcostcalcentryid)
      }
      this.formatBillAmount([]);
      if(numbers.length>0){
        introductionPrice(numbers).then(response => {
          let coefficientList = response.data;
          let bomCostcalcentryList = this.getTreeData(this.bomCostcalcentryList);
          for (let i = 0; i < bomCostcalcentryList.length; i++) {
            let listElement = bomCostcalcentryList[i];
            listElement.cftaxprice = 0;
            listElement.cfdeduction = 0;
            let rowTemp = this.materialBomDosageUpdate(listElement);
            bomCostcalcentryList[i] = rowTemp;
          }
          for (let i = 0; i < coefficientList.length; i++) {
            let coefficientListElement = coefficientList[i];
            let materialnumber = coefficientListElement.materialnumber;
            for (let j = 0; j < bomCostcalcentryList.length; j++) {
              let bomCostcalcentryListElement = bomCostcalcentryList[j];
              let cfmaterialhalfnumb1 = bomCostcalcentryListElement.cfmaterialhalfnumb;
              let cfbomcostcalcentryid = bomCostcalcentryListElement.cfbomcostcalcentryid;
              if(cfmaterialhalfnumb1===materialnumber&&longCode.includes(cfbomcostcalcentryid)){
                bomCostcalcentryListElement.cftaxrate = coefficientListElement.taxrate;
                bomCostcalcentryListElement.cftaxprice = coefficientListElement.price;
                bomCostcalcentryListElement.cfdeduction = coefficientListElement.price;
                let rowTemp = this.materialBomDosageUpdate(bomCostcalcentryListElement);
                bomCostcalcentryListElement = rowTemp;
              }
            }
          }
          this.formatBillAmount(bomCostcalcentryList);
        }).then(response =>{
          this.entryloading = false;
        }).catch(() => {
          this.entryloading = false;
        });
      }
      else{
        this.entryloading = false;
      }
    },
    /** 格式化单头和所有分录行的金额 */
    formatBillAmount(list){
      if(list.length!=0){
        this.bomCostcalcentryList = this.getTreeData(list);
      }else{
        this.bomCostcalcentryList = this.getTreeData(this.bomCostcalcentryList);
      }
      let bomCostcalcentryList = this.bomCostcalcentryList;
      let cfpackcost = 0;
      let cfalterationfee = 0;
      let cffixedfee = 0;
      let cftotalmaterialcost = 0;
      for (let i = 0; i < bomCostcalcentryList.length; i++) {
        let row = bomCostcalcentryList[i];
        let rowTemp = this.materialBomDosageUpdate(row);
        cfalterationfee+=(rowTemp.cfchangefee-0);
        cffixedfee+=(rowTemp.cffixationfee-0);
        cftotalmaterialcost+=(rowTemp.cfnotaxsum-0);
        if(rowTemp.cfrawmaterialid!=undefined&&rowTemp.cfrawmaterialid!=null&&rowTemp.cfrawmaterialid.length!=0&&rowTemp.cfrawmaterialid.substring(0, 2)=="50"){
          cfpackcost+=(rowTemp.cfnotaxsum-0);
        }
        row = rowTemp;
      }
      this.form.cfpackcost = this.formatAmountTwo(cfpackcost);
      this.form.cfalterationfee = this.formatAmountTwo(cfalterationfee);
      this.form.cffixedfee = this.formatAmountTwo(cffixedfee);
      this.form.cftotalmaterialcost = this.formatAmountTwo(cftotalmaterialcost);
      this.form.cftotalcost = this.formatAmountTwo((cfalterationfee-0)+(cffixedfee-0)+(cftotalmaterialcost-0));
      this.form.cftaxcost = this.form.cftotalcost*(1+this.returnTax(this.form.cftaxrate-0));
      this.form.cftaxcost= this.formatAmountTwo(this.form.cftaxcost);
      this.bomCostcalcentryList = this.formatBillTopNodeEntryAmount(this.bomCostcalcentryList);
      this.bomCostcalcentryList = this.setTreeDataIncludeOrdinalwidth(this.bomCostcalcentryList);
    },
    /** 格式化顶级节点为历史单的金额(旧版-只有历史BOM才会汇总子级的金额，新版是全会) */
    formatBillTopNodeEntryAmountPending(list){
      list = this.setTreeData(list);
      const arr = [1,3]
      for (let i = 0; i < list.length; i++) {
        let listElement = list[i];
        if (arr.includes(listElement.cfisbomcostcalcdetail)) {
          let cloneData = structuredClone(listElement);
          //获取cloneData的子级元素，改成平级
          let treeData = this.getTreeData(cloneData.children);
          //计算listElement的金额
          let rowTemp = this.materialBomDosageUpdate(listElement);
          let cfchangefee = rowTemp.cfchangefee;
          let cffixationfee = rowTemp.cffixationfee;
          let cfnotaxsum = rowTemp.cfnotaxsum;
          let isHaveSubordinates = false;
          for (let j = 0; j < treeData.length; j++) {
            isHaveSubordinates = true;
            let treeDatum = treeData[j];
            treeDatum = this.materialBomDosageUpdate(treeDatum);
            cfnotaxsum += this.formatAmountSix(treeDatum.cfdeduction*treeDatum.cfmaterialhalfdose/(1+this.returnTax(treeDatum.cftaxrate)));
            cfchangefee += this.formatAmountSix(treeDatum.cfrgbdxj+treeDatum.cfsbdxj+treeDatum.cfdbdxj+treeDatum.cfqbdxj+treeDatum.cfzjbdxj+treeDatum.cfqtbdxj+treeDatum.cfedexj);
            cffixationfee += this.formatAmountSix(treeDatum.cfrggdxj+treeDatum.cfsgdxj+treeDatum.cfdgdxj+treeDatum.cfqgdxj+treeDatum.cfzjgdxj+treeDatum.cfqtgdxj);
          }
          listElement.children = treeData;
          list[i].cfnotaxsum = this.formatAmountTen(cfnotaxsum);
          list[i].cfchangefee = this.formatAmountTwo(cfchangefee);
          list[i].cffixationfee = this.formatAmountTwo(cffixationfee);
          if(isHaveSubordinates){
            let structuredClone1 = structuredClone(list[i]);
            let treeData1 = this.getTreeData(structuredClone1.children);
            let children = this.formatBillTopNodeEntryAmount(treeData1);
            listElement.children = children;
          }
        }else if(listElement.cfisbomcostcalcdetail){
          let cloneData = structuredClone(listElement);
          //获取cloneData的子级元素，改成平级
          let treeData = this.getTreeData(cloneData.children);
          //计算listElement的金额
          let isHaveSubordinates = false;
          for (let j = 0; j < treeData.length; j++) {
            isHaveSubordinates = true;
            let treeDatum = treeData[j];
            treeDatum = this.materialBomDosageUpdate(treeDatum);
          }
          listElement.children = treeData;
          if(isHaveSubordinates){
            let structuredClone1 = structuredClone(list[i]);
            let treeData1 = this.getTreeData(structuredClone1.children);
            let children = this.formatBillTopNodeEntryAmount(treeData1);
            listElement.children = children;
          }
        }
      }
      list = this.getTreeData(list);
      return list;
    },
    /** 格式化顶级节点的金额 */
    formatBillTopNodeEntryAmount(list){
      list = this.setTreeData(list);
      for (let i = 0; i < list.length; i++) {
        let listElement = list[i];
        let cloneData = structuredClone(listElement);
        //获取cloneData的子级元素，改成平级
        let treeData = this.getTreeData(cloneData.children);
        //计算listElement的金额
        let rowTemp = this.materialBomDosageUpdate(listElement);
        let cfchangefee = rowTemp.cfchangefee;
        let cffixationfee = rowTemp.cffixationfee;
        let cfnotaxsum = rowTemp.cfnotaxsum;
        let isHaveSubordinates = false;
        for (let j = 0; j < treeData.length; j++) {
          isHaveSubordinates = true;
          let treeDatum = treeData[j];
          treeDatum = this.materialBomDosageUpdate(treeDatum);
          cfnotaxsum += this.formatAmountSix(treeDatum.cfdeduction*treeDatum.cfmaterialhalfdose/(1+this.returnTax(treeDatum.cftaxrate)));
          cfchangefee += this.formatAmountSix(treeDatum.cfrgbdxj+treeDatum.cfsbdxj+treeDatum.cfdbdxj+treeDatum.cfqbdxj+treeDatum.cfzjbdxj+treeDatum.cfqtbdxj+treeDatum.cfedexj);
          cffixationfee += this.formatAmountSix(treeDatum.cfrggdxj+treeDatum.cfsgdxj+treeDatum.cfdgdxj+treeDatum.cfqgdxj+treeDatum.cfzjgdxj+treeDatum.cfqtgdxj);
        }
        listElement.children = treeData;
        list[i].cfnotaxsum = this.formatAmountTen(cfnotaxsum);
        list[i].cfchangefee = this.formatAmountTwo(cfchangefee);
        list[i].cffixationfee = this.formatAmountTwo(cffixationfee);
        if(list[i].cfdeduction==null){
          list[i].cfdeduction = 0;
        }
        if(list[i].cftaxprice==null){
          list[i].cftaxprice = 0;
        }
        if(list[i].cfdeduction==0){
          list[i].cftaxprice = this.formatAmountSix(cfnotaxsum*(1+this.returnTax(list[i].cftaxrate))/ list[i].cfmaterialhalfdose);
          const value = Number(list[i].cftaxprice);
          if (isNaN(value) || value === 0 || !isFinite(value)) {
            list[i].cftaxprice = 0;
          }
        }
        if(isHaveSubordinates){
          let structuredClone1 = structuredClone(list[i]);
          let treeData1 = this.getTreeData(structuredClone1.children);
          let children = this.formatBillTopNodeEntryAmount(treeData1);
          listElement.children = children;
        }
      }
      list = this.getTreeData(list);
      return list;
    },
    /** 格式化顶级节点为历史单的金额(用量版本) */
    formatBillTopNodeEntryAmountToDose(list){
      list = this.setTreeData(list);
      for (let i = 0; i < list.length; i++) {
        let listElement = list[i];
        if(listElement.cfdose==0){
          listElement.cfdose = 1;
        }
        let magnification = (listElement.cfmaterialhalfdose-0)/(listElement.cfdose-0)
        let cloneData = structuredClone(listElement);
        //获取cloneData的子级元素，改成平级
        let treeData = this.getTreeData(cloneData.children);
        //计算listElement的金额
        let rowTemp = this.materialBomDosageUpdate(listElement);
        let cfchangefee = rowTemp.cfchangefee;
        let cffixationfee = rowTemp.cffixationfee;
        let cfnotaxsum = rowTemp.cfnotaxsum;
        let isHaveSubordinates = false;
        for (let j = 0; j < treeData.length; j++) {
          isHaveSubordinates = true;
          let treeDatum = treeData[j];
          treeDatum.cfmaterialhalfdose = treeDatum.cfdose*magnification+"";
          treeDatum = this.materialBomDosageUpdate(treeDatum);
          cfnotaxsum += this.formatAmountSix(treeDatum.cfdeduction*treeDatum.cfmaterialhalfdose/(1+this.returnTax(treeDatum.cftaxrate)));
          cfchangefee += this.formatAmountSix(treeDatum.cfrgbdxj+treeDatum.cfsbdxj+treeDatum.cfdbdxj+treeDatum.cfqbdxj+treeDatum.cfzjbdxj+treeDatum.cfqtbdxj+treeDatum.cfedexj);
          cffixationfee += this.formatAmountSix(treeDatum.cfrggdxj+treeDatum.cfsgdxj+treeDatum.cfdgdxj+treeDatum.cfqgdxj+treeDatum.cfzjgdxj+treeDatum.cfqtgdxj);
        }
        listElement.children = treeData;
        list[i].cfnotaxsum = this.formatAmountTen(cfnotaxsum);
        list[i].cfchangefee = this.formatAmountTwo(cfchangefee);
        list[i].cffixationfee = this.formatAmountTwo(cffixationfee);

        if(isHaveSubordinates){
          let structuredClone1 = structuredClone(list[i]);
          let treeData1 = this.getTreeData(structuredClone1.children);
          let children = this.formatBillTopNodeEntryAmountToDose(treeData1);
          listElement.children = children;
        }
      }
      list = this.getTreeData(list);
      return list;
    },
    /** 替换节点 */
    replaceNode(list,node){
      for (let i = 0; i < list.length; i++) {
        let listElement = list[i];
        if(listElement.cfdose==0){
          listElement.cfdose = 1;
        }
        if(listElement.cfbomcostcalcentryid==node[0].cfbomcostcalcentryid){
          list[i] = node[0];
          return list;
        }
        let cloneData = structuredClone(listElement);
        //获取cloneData的子级元素，改成平级
        let treeData = cloneData.children;
        if(treeData==undefined||treeData==null){
          treeData = [];
        }
        let isReplaceNode = true;
        for (let j = 0; j < treeData.length; j++) {
          let treeDatum = treeData[j];
          if(treeDatum.cfbomcostcalcentryid==node[0].cfbomcostcalcentryid&&isReplaceNode){
            if(node[0].children!=undefined&&node[0].children!=null&&node[0].children.length!=0){
              let structuredClone1 = structuredClone(node[0]);
              let treeData1 = this.getTreeData(structuredClone1.children);
              let children = this.formatBillTopNodeEntryAmountToDose(treeData1);
              node[0].children = children;
            }
            listElement[j] = node[0];
            list[i] = listElement;
            isReplaceNode = false;
            return list;
          }
        }
      }
      return list;
    },
    /** 格式化分录金额 并构造新对象*/
    materialBomDosageUpdate(row){
      if(row!=null){
        let rowTemp = row;
        if(rowTemp.cfcategoryname==null||rowTemp.cfcategoryname==""||rowTemp.cfcategoryname==undefined){
          rowTemp.cfcategoryid = null;
          rowTemp.cfcategoryname = null;
          rowTemp.cfrgbd = 0;
          rowTemp.cfrggd = 0;
          rowTemp.cfsbd = 0;
          rowTemp.cfsgd = 0;
          rowTemp.cfdbd = 0;
          rowTemp.cfdgd = 0;
          rowTemp.cfqbd = 0;
          rowTemp.cfqgd = 0;
          rowTemp.cfzjbd = 0;
          rowTemp.cfzjgd = 0;
          rowTemp.cfqtbd = 0;
          rowTemp.cfqtgd = 0;
          rowTemp.cfede = 0;
        }
        if(rowTemp.cfmaterialhalfdose==null||rowTemp.cfmaterialhalfdose==undefined||rowTemp.cfmaterialhalfdose==""){
          rowTemp.cfdose = 0;
          rowTemp.cfmaterialhalfdose = 0;
        }
        rowTemp.cfrgbdxj = this.formatAmountTwo(rowTemp.cfrgbd*rowTemp.cfmaterialhalfdose);
        rowTemp.cfrggdxj = this.formatAmountTwo(rowTemp.cfrggd*rowTemp.cfmaterialhalfdose);
        rowTemp.cfsbdxj = this.formatAmountTwo(rowTemp.cfsbd*rowTemp.cfmaterialhalfdose);
        rowTemp.cfsgdxj = this.formatAmountTwo(rowTemp.cfsgd*rowTemp.cfmaterialhalfdose);
        rowTemp.cfdbdxj = this.formatAmountTwo(rowTemp.cfdbd*rowTemp.cfmaterialhalfdose);
        rowTemp.cfdgdxj = this.formatAmountTwo(rowTemp.cfdgd*rowTemp.cfmaterialhalfdose);
        rowTemp.cfqbdxj = this.formatAmountTwo(rowTemp.cfqbd*rowTemp.cfmaterialhalfdose);
        rowTemp.cfqgdxj = this.formatAmountTwo(rowTemp.cfqgd*rowTemp.cfmaterialhalfdose);
        rowTemp.cfzjbdxj = this.formatAmountTwo(rowTemp.cfzjbd*rowTemp.cfmaterialhalfdose);
        rowTemp.cfzjgdxj = this.formatAmountTwo(rowTemp.cfzjgd*rowTemp.cfmaterialhalfdose);
        rowTemp.cfqtbdxj = this.formatAmountTwo(rowTemp.cfqtbd*rowTemp.cfmaterialhalfdose);
        rowTemp.cfqtgdxj = this.formatAmountTwo(rowTemp.cfqtgd*rowTemp.cfmaterialhalfdose);
        rowTemp.cfedexj = this.formatAmountTwo(rowTemp.cfede*rowTemp.cfmaterialhalfdose);
        rowTemp.cfnotaxsum = this.formatAmountTen(rowTemp.cfdeduction*rowTemp.cfmaterialhalfdose/(1+this.returnTax(rowTemp.cftaxrate)));
        rowTemp.cfchangefee = this.formatAmountTwo(rowTemp.cfrgbdxj+rowTemp.cfsbdxj+rowTemp.cfdbdxj+rowTemp.cfqbdxj+rowTemp.cfzjbdxj+rowTemp.cfqtbdxj+rowTemp.cfedexj);
        rowTemp.cffixationfee = this.formatAmountTwo(rowTemp.cfrggdxj+rowTemp.cfsgdxj+rowTemp.cfdgdxj+rowTemp.cfqgdxj+rowTemp.cfzjgdxj+rowTemp.cfqtgdxj);
        return rowTemp;
      }
    },
    /** 格式化分录金额 并构造新对象*/
    copyEntry(row){
      if(row!=null){
        let rowTemp = row;
        if(rowTemp.cfcategoryname==null||rowTemp.cfcategoryname==""||rowTemp.cfcategoryname==undefined){
          rowTemp.cfcategoryid = null;
          rowTemp.cfcategoryname = null;
          rowTemp.cfrgbd = 0;
          rowTemp.cfrggd = 0;
          rowTemp.cfsbd = 0;
          rowTemp.cfsgd = 0;
          rowTemp.cfdbd = 0;
          rowTemp.cfdgd = 0;
          rowTemp.cfqbd = 0;
          rowTemp.cfqgd = 0;
          rowTemp.cfzjbd = 0;
          rowTemp.cfzjgd = 0;
          rowTemp.cfqtbd = 0;
          rowTemp.cfqtgd = 0;
          rowTemp.cfede = 0;
        }
        if(rowTemp.cfmaterialhalfdose==null||rowTemp.cfmaterialhalfdose==undefined||rowTemp.cfmaterialhalfdose==""){
          rowTemp.cfdose = 0;
          rowTemp.cfmaterialhalfdose = 0;
        }
        rowTemp.cfrgbdxj = this.formatAmountTwo(rowTemp.cfrgbd*rowTemp.cfmaterialhalfdose);
        rowTemp.cfrggdxj = this.formatAmountTwo(rowTemp.cfrggd*rowTemp.cfmaterialhalfdose);
        rowTemp.cfsbdxj = this.formatAmountTwo(rowTemp.cfsbd*rowTemp.cfmaterialhalfdose);
        rowTemp.cfsgdxj = this.formatAmountTwo(rowTemp.cfsgd*rowTemp.cfmaterialhalfdose);
        rowTemp.cfdbdxj = this.formatAmountTwo(rowTemp.cfdbd*rowTemp.cfmaterialhalfdose);
        rowTemp.cfdgdxj = this.formatAmountTwo(rowTemp.cfdgd*rowTemp.cfmaterialhalfdose);
        rowTemp.cfqbdxj = this.formatAmountTwo(rowTemp.cfqbd*rowTemp.cfmaterialhalfdose);
        rowTemp.cfqgdxj = this.formatAmountTwo(rowTemp.cfqgd*rowTemp.cfmaterialhalfdose);
        rowTemp.cfzjbdxj = this.formatAmountTwo(rowTemp.cfzjbd*rowTemp.cfmaterialhalfdose);
        rowTemp.cfzjgdxj = this.formatAmountTwo(rowTemp.cfzjgd*rowTemp.cfmaterialhalfdose);
        rowTemp.cfqtbdxj = this.formatAmountTwo(rowTemp.cfqtbd*rowTemp.cfmaterialhalfdose);
        rowTemp.cfqtgdxj = this.formatAmountTwo(rowTemp.cfqtgd*rowTemp.cfmaterialhalfdose);
        rowTemp.cfedexj = this.formatAmountTwo(rowTemp.cfede*rowTemp.cfmaterialhalfdose);
        rowTemp.cfnotaxsum = this.formatAmountTen(rowTemp.cfdeduction*rowTemp.cfmaterialhalfdose/(1+this.returnTax(rowTemp.cftaxrate)));
        rowTemp.cfchangefee = this.formatAmountTwo(rowTemp.cfrgbdxj+rowTemp.cfsbdxj+rowTemp.cfdbdxj+rowTemp.cfqbdxj+rowTemp.cfzjbdxj+rowTemp.cfqtbdxj+rowTemp.cfedexj);
        rowTemp.cffixationfee = this.formatAmountTwo(rowTemp.cfrggdxj+rowTemp.cfsgdxj+rowTemp.cfdgdxj+rowTemp.cfqgdxj+rowTemp.cfzjgdxj+rowTemp.cfqtgdxj);
        return rowTemp;
      }
    },
    /** 清空分录系数 */
    clearEntryFactor(row){
      if(row!=null){
        let rowTemp = row;
        rowTemp.cfrgbd = 0;
        rowTemp.cfrggd = 0;
        rowTemp.cfsbd = 0;
        rowTemp.cfsgd = 0;
        rowTemp.cfdbd = 0;
        rowTemp.cfdgd = 0;
        rowTemp.cfqbd = 0;
        rowTemp.cfqgd = 0;
        rowTemp.cfzjbd = 0;
        rowTemp.cfzjgd = 0;
        rowTemp.cfqtbd = 0;
        rowTemp.cfqtgd = 0;
        rowTemp.cfede = 0;

        rowTemp.cfrgbdxj = rowTemp.cfrgbd*rowTemp.cfmaterialhalfdose;
        rowTemp.cfrggdxj = rowTemp.cfrggd*rowTemp.cfmaterialhalfdose;
        rowTemp.cfsbdxj = rowTemp.cfsbd*rowTemp.cfmaterialhalfdose;
        rowTemp.cfsgdxj = rowTemp.cfsgd*rowTemp.cfmaterialhalfdose;
        rowTemp.cfdbdxj = rowTemp.cfdbd*rowTemp.cfmaterialhalfdose;
        rowTemp.cfdgdxj = rowTemp.cfdgd*rowTemp.cfmaterialhalfdose;
        rowTemp.cfqbdxj = rowTemp.cfqbd*rowTemp.cfmaterialhalfdose;
        rowTemp.cfqgdxj = rowTemp.cfqgd*rowTemp.cfmaterialhalfdose;
        rowTemp.cfzjbdxj = rowTemp.cfzjbd*rowTemp.cfmaterialhalfdose;
        rowTemp.cfzjgdxj = rowTemp.cfzjgd*rowTemp.cfmaterialhalfdose;
        rowTemp.cfqtbdxj = rowTemp.cfqtbd*rowTemp.cfmaterialhalfdose;
        rowTemp.cfqtgdxj = rowTemp.cfqtgd*rowTemp.cfmaterialhalfdose;
        rowTemp.cfedexj = rowTemp.cfede*rowTemp.cfmaterialhalfdose;
        rowTemp.cfnotaxsum = this.formatAmountTen(rowTemp.cfdeduction*rowTemp.cfmaterialhalfdose/(1+this.returnTax(rowTemp.cftaxrate)));
        rowTemp.cfchangefee = this.formatAmountTwo(rowTemp.cfrgbdxj+rowTemp.cfsbdxj+rowTemp.cfdbdxj+rowTemp.cfqbdxj+rowTemp.cfzjbdxj+rowTemp.cfqtbdxj+rowTemp.cfedexj);
        rowTemp.cffixationfee = this.formatAmountTwo(rowTemp.cfrggdxj+rowTemp.cfsgdxj+rowTemp.cfdgdxj+rowTemp.cfqgdxj+rowTemp.cfzjgdxj+rowTemp.cfqtgdxj);
        return rowTemp;
      }
    },
    formatEntry(row,type){
      if("prcie"==type){
        let bomCostcalcentryList = this.getLeaves(this.bomCostcalcentryList);
        for (let i = 0; i < bomCostcalcentryList.length; i++) {
          let listElement = bomCostcalcentryList[i];
          if (listElement.index == row.index) {
            row.cfdeduction = row.cftaxprice;
          }
        }
      }
      row = this.materialBomDosageUpdate(row);
      row.cfmaterialhalfdose = this.formatAmountSeven(row.cfmaterialhalfdose);
      let magnification = (row.cfmaterialhalfdose-0)/(row.cfdose-0)
      const value = Number(magnification);
      if (isNaN(value) || !isFinite(value)) {
        magnification = 1;
      }
      magnification = this.formatAmountTen(magnification);
      this.bomCostcalcentryList = this.getTreeData(this.bomCostcalcentryList);
      for (let i = 0; i < this.bomCostcalcentryList.length; i++) {
        let listElement = this.bomCostcalcentryList[i];
        if((listElement.cfbomcostcalcentryid+"").includes(row.cfbomcostcalcentryid+"")){
          if(listElement.cfbomcostcalcentryid==row.cfbomcostcalcentryid){
            this.bomCostcalcentryList[i] = row;
            continue
          }
          if(listElement.cfdose==undefined||listElement.cfdose==null||listElement.cfdose==''){
            listElement.cfdose=0;
          }
          let cfmaterialhalfdose = this.formatAmountSeven(listElement.cfdose*magnification);
          listElement.cfmaterialhalfdose = cfmaterialhalfdose;
        }
      }
      this.bomCostcalcentryList = this.formatBillTopNodeEntryAmount(this.bomCostcalcentryList);
      this.bomCostcalcentryList = this.setTreeDataIncludeOrdinalwidth(this.bomCostcalcentryList);
      //记得格式化单头金额
      this.formatBillAmount([]);
    },
    returnTax(tax){
      if(tax>=1){
        return tax/100;
      }else if(tax==null){
        return 0;
      }else{
        return tax;
      }
    },
    formatAmountSix(amount){
      if(amount==undefined||amount==NaN){
        amount = 0;
      }
      let number = parseFloat(parseFloat(amount).toFixed(6));
      return this.tailRemoval(number)
    },
    formatAmountTwo(amount){
      if(amount==undefined||amount==NaN){
        amount = 0;
      }
      let number = parseFloat(parseFloat(amount).toFixed(2));
      return this.tailRemoval(number)
    },
    formatAmountTen(amount){
      if(amount==undefined||amount==NaN){
        amount = 0;
      }
      let number = parseFloat(parseFloat(amount).toFixed(12));
      return this.tailRemoval(number)
    },
    formatAmountSeven(amount){
      if(amount==undefined||amount==NaN){
        amount = 0;
      }
      let number = parseFloat(parseFloat(amount).toFixed(7));
      return this.tailRemoval(number)
    },
    tailRemoval(number){
      for (let i = number.length-1; i >= 0; i--) {
        if (number[i] == 0) {
          number = number.substring(0,i);
        }else if(number[i] == '.'){
          number = number.substring(0,i);
          break;
        }else{
          break;
        }
      }
      return number
    },
    selectCategoryAdjustmentFactor(cfcategory,row){
      this.entryloading = true;
      this.basedatadialog.category.queryParams.cfcategory = cfcategory;
      this.basedatadialog.category.queryParams.cfwfzbaseid = this.form.cfwfzbaseid;
      this.basedatadialog.category.queryParams.cfseasontypeid = this.form.cfseasontypeid;
      this.basedatadialog.category.queryParams.cfisnew = "1";
      if(cfcategory==null||cfcategory==""||this.form.cfwfzbaseid==null||this.form.cfwfzbaseid==""||this.form.cfseasontypeid==null||this.form.cfseasontypeid==""){
        let source = this.bomCostcalcentryList;
        this.formatBillAmount(source);
        return;
      }
      listCoefficient(this.basedatadialog.category.queryParams).then(response => {
        let coefficientList = response.rows;
        if(coefficientList.length!=0){
          let coeff = coefficientList[0];
          row.cfrgbd = coeff.cfrgbd;
          row.cfrggd = coeff.cfrggd;
          row.cfsbd = coeff.cfsbd;
          row.cfsgd = coeff.cfsgd;
          row.cfdbd = coeff.cfdbd;
          row.cfdgd = coeff.cfdgd;
          row.cfqbd = coeff.cfqbd;
          row.cfqgd = coeff.cfqgd;
          row.cfzjbd = coeff.cfzjbd;
          row.cfzjgd = coeff.cfzjgd;
          row.cfqtbd = coeff.cfqtbd;
          row.cfqtgd = coeff.cfqtgd;
          row.cfede = coeff.cfede;
        }else{
          row.cfrgbd = 0;
          row.cfrggd = 0;
          row.cfsbd = 0;
          row.cfsgd = 0;
          row.cfdbd = 0;
          row.cfdgd = 0;
          row.cfqbd = 0;
          row.cfqgd = 0;
          row.cfzjbd = 0;
          row.cfzjgd = 0;
          row.cfqtbd = 0;
          row.cfqtgd = 0;
          row.cfede = 0;
        }
        let source = this.bomCostcalcentryList;
        source = this.getTreeData(source);
        for (let i = 0; i < source.length; i++) {
          if (source[i].cfbomcostcalcentryid == row.cfbomcostcalcentryid) {
            row = this.materialBomDosageUpdate(row);
            if(source[i].children==undefined||source[i].children.length==0){
              row.children = [];
            }
            source[i] = row;
            break;
          }
        }
        this.bomCostcalcentryList = source;
        this.formatBillAmount(this.bomCostcalcentryList);
        this.entryloading = false;
      });
    },
    assignIndexes(data, parentIndex, startIndex) {
      let index = startIndex;
      let list = [];
      data.forEach(item => {
        const newIndex = parentIndex ? `${parentIndex}.${index}` : `${index}`;
        item.index = newIndex;
        let number = (newIndex+"").split('.').length;
        item.level = number;
        list.push(item);
        if (item.children && item.children.length > 0) {
          this.assignIndexes(item.children, newIndex, 1);
        }
        index++;
      });
      return list;
    },
    calculateIndexColumnWidth(list) {
      let maxLength = 0;
      list.forEach(item => {
        if (item.index!=undefined&&item.index!=null&&item.index.length!=undefined&&item.index.length!=null&&item.index.length > maxLength) {
          maxLength = item.index.length;
        }
      });
      // 假设每个字符占用 10px 的宽度
      this.indexColumnWidth = maxLength * 11 + 50; // 加上一些额外的空间
    },
    setTreeDataIncludeOrdinalwidth(source){
      source = this.setTreeData(source);
      source = this.getTreeData(source);
      this.calculateIndexColumnWidth(source);
      source = this.setTreeData(source);
      this.bomCostcalcentryList2 = this.filterList(source);
      return source;
    },
    setTreeData(source){
      let filter = this.handleTree(source, "cfbomcostcalcentryid", "cfrawmaterialnumbe");
      filter = this.assignIndexes(filter, '', 1);
      return  filter;
    },
    getTreeData(source){
      let list = [];
      if(source==undefined){
        source = [];
      }
      let cloneData = structuredClone(source);
      for (let i = 0; i < cloneData.length; i++) {
        let children = cloneData[i];
        this.recursivelyGetChildren(children, list);
      }
      for (let i = 0; i < list.length; i++) {
        list[i].children = [];
      }
      return list;
    },
    recursivelyGetChildren(children, list) {
      if (children.children && children.children.length > 0) {
        for (let i = 0; i < children.children.length; i++) {
          this.recursivelyGetChildren(children.children[i], list);
        }
        children.children = [];
      }
      list.push(children);
    },
    /** 上查按钮操作 */
    handleCheckBelow(row) {
      const fids = this.cfcalcdemandid;
      var fid = fids[0];
      this.basedatadialog.f7dataType = "calcdemand";
      this.basedatadialog.calcdemand.show = !this.basedatadialog.calcdemand.show;
      this.basedatadialog.calcdemand.fid = fid;
    },
    /** 展开/折叠操作 */
    toggleExpandAll() {
      this.refreshTable = false;
      this.isExpandAll = !this.isExpandAll;
      this.$nextTick(() => {
        this.refreshTable = true;
      });
    },
    materialNumberChange(row){
      if(this.form.cfseasontypeid==null||this.form.cfseasontypeid==""||this.form.cfwfzbaseid==null||this.form.cfwfzbaseid==""){
        this.$modal.msgError("请先选择时间类型和基地!");
        row.cfmaterialhalfnumb = "";
        return;
      }
      if(row.cfbomcostcalcnumbe==="0"){
        let queryParams = {
          cfstate: "AUDIT",
          cfmaterialid: row.cfmaterialhalfnumb
        }
        listCostcalc(queryParams).then(response => {
          this.basedatadialog.F7dataType = 'material';
          this.basedatadialog.materialhalf.row = row;
          this.basedatadialog.costcalclist.isEntry = true;
          this.basedatadialog.costcalclist.cfmaterialhalfnumb = row.cfmaterialhalfnumb;
          if(1!=response.rows.length){
            this.basedatadialog.costcalclist.show = !this.basedatadialog.costcalclist.show;
          }else{
            let row1 = response.rows[0];
            getCostcalc(row1.fid).then(response => {
              row1.bomCostcalcentryList = response.data.bomCostcalcentryList;
              let data={
                costcalclist : row1
              }
              this.getF7Data(data)
            });
          }
        });
      }
      else if(row.cfbomcostcalcnumbe==="1"){
        let queryParams = {
          cfmaterialnumber: row.cfmaterialhalfnumb
        }
        listMaterialhalf(queryParams).then(response => {
          this.basedatadialog.F7dataType = 'material';
          this.basedatadialog.materialhalf.row = row;
          this.basedatadialog.materialhalf.isEntry = true;
          this.basedatadialog.materialhalf.cfmaterialnumber = row.cfmaterialhalfnumb;
          if(1!=response.rows.length){
            this.basedatadialog.materialhalf.show = !this.basedatadialog.materialhalf.show;
          }else{
            let row1 = response.rows[0];
            let data={
              materialhalf : row1
            }
            this.getF7Data(data)
          }
        });
      }
      else if(row.cfbomcostcalcnumbe==="2"){
        let queryParams = {
          materialnumber: row.cfmaterialhalfnumb
        }
        listBomsm(queryParams).then(response => {
          this.basedatadialog.F7dataType = 'material';
          this.basedatadialog.materialhalf.row = row;
          this.basedatadialog.specialmaterial.isSpecial = true;
          this.basedatadialog.specialmaterial.isspecialmaterial = "1";
          this.basedatadialog.specialmaterial.number = row.cfmaterialhalfnumb;
          if(1!=response.rows.length){
            this.basedatadialog.specialmaterial.show = !this.basedatadialog.specialmaterial.show;
          }else{
            let row1 = response.rows[0];
            let data={
              specialmaterial : row1
            }
            this.getF7Data(data)
          }
        });
      }
      else{
        row.cfmaterialhalfnumb = "";
        return;
      }
    },
    removeBillMaterial(){
      this.form.cfmaterialname = null;
      this.form.cfbaseunit = null;
      this.form.cfmodel = null;
      this.form.cftaxrate = null;
      this.form.fhandlerid = null;
      this.formatBillAmount([]);
    },
    changeSelection(type){
      if(type=="wfzbase"){
        this.$refs["form"].validateField("cfwfzbaseid");
      }else{
        this.$refs["form"].validateField("cfseasontypeid");
      }
    },
    /** f7点击事件 */
    openF7(show,isEntry,row,isSpecial) {
      this.basedatadialog.F7dataType = show;
      switch (show) {
        case 'costcenter':
          this.basedatadialog.costcenter.show = !this.basedatadialog.costcenter.show;
          break;
        case 'customer':
          this.basedatadialog.customer.show = !this.basedatadialog.customer.show;
          break;
        case 'material':
          if(this.form.cfseasontypeid==null||this.form.cfseasontypeid==""||this.form.cfwfzbaseid==null||this.form.cfwfzbaseid==""){
            this.$modal.msgError("请先选择时间类型和基地!");
            return;
          }
          if(row.cfbomcostcalcnumbe==="0"){
            this.basedatadialog.materialhalf.row = row;
            this.basedatadialog.costcalclist.isEntry = true;
            this.basedatadialog.costcalclist.cfmaterialhalfnumb = null;
            this.basedatadialog.costcalclist.show = !this.basedatadialog.costcalclist.show;
          }else if(row.cfbomcostcalcnumbe==="1"){
            this.basedatadialog.materialhalf.row = row;
            this.basedatadialog.materialhalf.isEntry = true;
            this.basedatadialog.materialhalf.cfmaterialnumber = null;
            this.basedatadialog.materialhalf.show = !this.basedatadialog.materialhalf.show;
          }else if(row.cfbomcostcalcnumbe==="2"){
            this.basedatadialog.materialhalf.row = row;
            this.basedatadialog.specialmaterial.isSpecial = true;
            this.basedatadialog.specialmaterial.isspecialmaterial = "0";
            this.basedatadialog.specialmaterial.number = null;
            this.basedatadialog.specialmaterial.show = !this.basedatadialog.specialmaterial.show;
          }else{
            this.$modal.msgError("请选择数据来源!");
          }
          break;
        case 'category':
          if(this.form.cfseasontypeid==null||this.form.cfseasontypeid==""||this.form.cfwfzbaseid==null||this.form.cfwfzbaseid==""){
            this.$modal.msgError("请先选择时间类型和基地!");
            return;
          }
          if(isEntry){
            this.basedatadialog.category.row = row;
            this.basedatadialog.category.isEntry = true;
          }
          this.basedatadialog.category.show = !this.basedatadialog.category.show;
          break;
        case 'specialmaterial':
          if(isSpecial){
            this.basedatadialog.specialmaterial.row = row;
            this.basedatadialog.specialmaterial.isSpecial = true;
            this.basedatadialog.specialmaterial.isspecialmaterial = "1";
          }else{
            this.basedatadialog.specialmaterial.isSpecial = false;
            this.basedatadialog.specialmaterial.isspecialmaterial = "";
          }
          this.basedatadialog.specialmaterial.show = !this.basedatadialog.specialmaterial.show;
          break;
      }
    },
    /** f7获取值 */
    getF7Data(data) {
      switch (this.basedatadialog.F7dataType) {
        case "costcenter":
          if (data.costcenter != null) {
            this.form.deptName = data.costcenter.name;
            this.form.fsourcefunction = data.costcenter.number;
          }else{
            this.form.deptName = null;
            this.form.fsourcefunction = null;
          }
          this.$refs["form"].validateField("costcenter");
          break;
        case "customer":
          if (data.customer != null) {
            this.form.cfcustomerid = data.customer.customername;
          }else{
            this.form.cfcustomerid = null;
          }
          break;
        case "category":
          if (data.category != null) {
            this.basedatadialog.category.row.cfcategoryid = data.category.id;
            this.basedatadialog.category.row.cfcategoryname = data.category.name;
            this.basedatadialog.category.row.cfworkshopname = data.category.cfplantid;
            this.selectCategoryAdjustmentFactor(data.category.id,this.basedatadialog.category.row);
          }else{
            this.basedatadialog.category.row.cfcategoryid = "";
            this.basedatadialog.category.row.cfcategoryname = "";
            let clearEntryFactor1 = this.clearEntryFactor(this.basedatadialog.category.row);
            this.basedatadialog.category.row = clearEntryFactor1;
            this.formatBillAmount([]);
          }
          break;
        case "material":
          this.entryloading = true;
          if(this.basedatadialog.materialhalf.row.children != undefined){
            this.basedatadialog.materialhalf.row.children = [];
          }
          if(this.basedatadialog.materialhalf.row.cfbomcostcalcnumbe==="0"){
            if (data.costcalclist != null) {
              if(data.costcalclist.fid==this.form.fid){
                this.$modal.msgError("历史BOM不能选择自己!");
                this.entryloading = false;
                return;
              }
              let Firsttimetoputtopmaterial = true;
              let objACopy = null;
              let longcode = "";
              let longcode2 = null;
              let bomCostcalcentryList = data.costcalclist.bomCostcalcentryList;
              let array = [1,3]
              for (let i = bomCostcalcentryList.length-1; i >= 0; i--) {
                bomCostcalcentryList[i].cfbomcostcalcnumbe = "0";
                if(array.includes(bomCostcalcentryList[i].cfisbomcostcalcdetail)){
                  bomCostcalcentryList[i].cfisbomcostcalcdetail = 3;
                }else{
                  bomCostcalcentryList[i].cfisbomcostcalcdetail = 2;
                }
                bomCostcalcentryList[i].cfdose = bomCostcalcentryList[i].cfmaterialhalfdose;
                if(!bomCostcalcentryList[i].cfbomcostcalcentryid.includes(",")){
                  longcode += bomCostcalcentryList[i].cfbomcostcalcentryid+",";
                  if(Firsttimetoputtopmaterial){
                    objACopy = { ...bomCostcalcentryList[i] };
                    this.basedatadialog.materialhalf.row.cfbomcostcalcentryid = bomCostcalcentryList[i].cfbomcostcalcentryid;
                    longcode2 = bomCostcalcentryList[i].cfbomcostcalcentryid;
                    bomCostcalcentryList.splice(i, 1);
                    Firsttimetoputtopmaterial = false;
                  }
                }
              }
              this.bomCostcalcentryList = this.getTreeData(this.bomCostcalcentryList);
              let index = this.bomCostcalcentryList.findIndex((item) => item.cfbomcostcalcentryid === longcode2);
              this.$set(this.bomCostcalcentryList,index,objACopy);
              this.bomCostcalcentryList = this.bomCostcalcentryList.concat(bomCostcalcentryList);
              this.form.longCode = longcode;
              this.bomCostcalcentryList = this.setTreeData(this.bomCostcalcentryList);
              let source = this.filterList(this.bomCostcalcentryList);
              this.form.bomCostcalcentryList = this.getTreeData(source);
              // this.basedatadialog.materialhalf.row.cfmaterialhalfnumb = data.costcalclist.cfmaterialid;
              // this.basedatadialog.materialhalf.row.cfmaterialhalfname = data.costcalclist.cfmaterialname;
              // this.basedatadialog.materialhalf.row.cfmaterialhalfcode = data.costcalclist.fid;
              // this.basedatadialog.materialhalf.row.cfmaterialhalfunit = data.costcalclist.cfbaseunit;
              // this.basedatadialog.materialhalf.row.cffixationfee = data.costcalclist.cffixedfee;
              // this.basedatadialog.materialhalf.row.cfchangefee = data.costcalclist.cfalterationfee;
              // this.basedatadialog.materialhalf.row.cftaxrate = data.costcalclist.cftaxrate;
              // this.basedatadialog.materialhalf.row.cfmaterialhalfdose = 1;
              // this.basedatadialog.materialhalf.row.cfdose = 1;
              // this.basedatadialog.materialhalf.row.cfisbomcostcalcdetail = 1;
              // let bomCostcalcentryList = data.costcalclist.bomCostcalcentryList;
              // let array = [1,3]
              // for (let i = 0; i < bomCostcalcentryList.length; i++) {
              //   bomCostcalcentryList[i].cfbomcostcalcnumbe = this.basedatadialog.materialhalf.row.cfbomcostcalcnumbe;
              //   if(array.includes(bomCostcalcentryList[i].cfisbomcostcalcdetail)){
              //     bomCostcalcentryList[i].cfisbomcostcalcdetail = 3;
              //   }else{
              //     bomCostcalcentryList[i].cfisbomcostcalcdetail = 2;
              //   }
              //   bomCostcalcentryList[i].cfbomcostcalcentryid = this.basedatadialog.materialhalf.row.cfbomcostcalcentryid+","+bomCostcalcentryList[i].cfbomcostcalcentryid;
              //   let cfrawmaterialnumbe = bomCostcalcentryList[i].cfrawmaterialnumbe;
              //   if(cfrawmaterialnumbe!=null&&cfrawmaterialnumbe.length!=0){
              //     bomCostcalcentryList[i].cfrawmaterialnumbe = this.basedatadialog.materialhalf.row.cfbomcostcalcentryid+","+ cfrawmaterialnumbe;
              //   }else{
              //     bomCostcalcentryList[i].cfrawmaterialnumbe = this.basedatadialog.materialhalf.row.cfbomcostcalcentryid;
              //   }
              //   bomCostcalcentryList[i].cfdose = bomCostcalcentryList[i].cfmaterialhalfdose;
              // }
              // this.bomCostcalcentryList = this.getTreeData(this.bomCostcalcentryList);
              // this.bomCostcalcentryList = this.bomCostcalcentryList.concat(bomCostcalcentryList);
              // this.form.longCode = this.basedatadialog.materialhalf.row.cfbomcostcalcentryid;
              // this.bomCostcalcentryList = this.setTreeData(this.bomCostcalcentryList);
              // let source = this.filterList(this.bomCostcalcentryList);
              // this.form.bomCostcalcentryList = this.getTreeData(source);
              getBomCoffe(this.form).then(response => {
                let list = response.list;
                this.bomCostcalcentryList = this.getTreeData(this.bomCostcalcentryList);
                if(list!=undefined&&list!=null&&list.length!=0){
                  for (let i = 0; i < list.length; i++) {
                    let listElement = list[i];
                    for (let j = 0; j < this.bomCostcalcentryList.length; j++) {
                      let bomCostcalcentryListElement = this.bomCostcalcentryList[j];
                      if(bomCostcalcentryListElement.cfbomcostcalcentryid==listElement.cfbomcostcalcentryid){
                        let row = this.materialBomDosageUpdate(listElement);
                        this.bomCostcalcentryList[j] = row;
                      }
                    }
                  }
                }
                this.formatBillAmount(this.bomCostcalcentryList);
              }).then(response =>{
                this.basedatadialog.materialhalf.queryParams.materialbomname = this.basedatadialog.materialhalf.row.cfmaterialhalfname;
                this.basedatadialog.materialhalf.queryParams.materialbomnumber = this.basedatadialog.materialhalf.row.cfmaterialhalfnumb;
                listAsscategory(this.basedatadialog.materialhalf.queryParams).then(response => {
                  let asscategoryList = response.rows;
                  if(asscategoryList.length!=0){
                    let asscategoryListElement = asscategoryList[0];
                    this.basedatadialog.materialhalf.row.cfcategoryid = asscategoryListElement.categoryid;
                    this.basedatadialog.materialhalf.row.cfcategoryname = asscategoryListElement.categoryname;
                    this.selectCategoryAdjustmentFactor(asscategoryListElement.categoryid,this.basedatadialog.materialhalf.row);
                    this.entryloading = false;
                  }else{
                    this.formatBillAmount([])
                  }
                })
              });
              this.entryloading = false;
            }
            else{
              this.switchDataSource(this.basedatadialog.materialhalf.row);
              this.entryloading = false;
            }
          }
          else if(this.basedatadialog.materialhalf.row.cfbomcostcalcnumbe==="1"){
            if (data.materialhalf != null) {
              this.basedatadialog.materialhalf.row.cfmaterialhalfnumb = data.materialhalf.cfmaterialnumber;
              this.basedatadialog.materialhalf.row.cfmaterialhalfcode = data.materialhalf.fid;
              this.basedatadialog.materialhalf.row.cfmaterialhalfname = data.materialhalf.cfmaterialname;
              this.basedatadialog.materialhalf.row.cfmaterialhalfunit = data.materialhalf.cfmaterialunit;
              this.basedatadialog.materialhalf.row.cfmaterialhalfdose = data.materialhalf.cfproductionquantity;
              this.basedatadialog.materialhalf.row.cfdose = data.materialhalf.cfproductionquantity;
              this.basedatadialog.materialhalf.row.cfisbomcostcalcdetail = 4;
              this.basedatadialog.materialhalf.category = data.materialhalf.cfcategoryid;
              getMaterialBomAllNodes(data.materialhalf.fid,this.basedatadialog.materialhalf.row.cfbomcostcalcentryid).then(response => {
                if(response.reason!=null&&response.reason.length!=0){
                  this.$message.error(response.reason);
                  this.entryloading = false;
                  return [];
                }
                let list = response.list;
                if (list==null||list<=0) {
                  this.entryloading = false;
                  return [];
                }else if(response.reason){
                  this.$modal.msgError(response.reason);
                }
                this.basedatadialog.materialhalf.list = list;
              })
                .then(response => {
                  let bomMaterialhalfentryList = this.basedatadialog.materialhalf.list;
                  this.basedatadialog.materialhalf.list = [];
                  let bomCostcalcentryList1 = this.getTreeData(this.bomCostcalcentryList);
                  for (let i = 0; i < bomMaterialhalfentryList.length; i++) {
                    let rowList = bomMaterialhalfentryList[i];
                    let obj = this.creatNewEntry();
                    obj.cfbomcostcalcnumbe = this.basedatadialog.materialhalf.row.cfbomcostcalcnumbe;
                    obj.cfmaterialhalfnumb = rowList.cfrawmaterialnumbe;
                    obj.cfmaterialhalfname = rowList.cfrawmaterialname;
                    obj.cfmaterialhalfunit = rowList.cfrawmaterialunit;
                    obj.cfmaterialhalfdose = rowList.cfconsumption;
                    obj.cftaxrate = rowList.cftaxrate;
                    obj.cfisbomcostcalcdetail = 4;
                    obj.cfdose = rowList.cfconsumption;
                    obj.cfrawmaterialnumbe = rowList.paramnumber;
                    obj.cfbomcostcalcentryid = rowList.longCode;
                    bomCostcalcentryList1.push(obj);
                  }
                  //查询物料分类和税率
                  //查询bom类别关联表
                  //查询系数
                  this.form.longCode = this.basedatadialog.materialhalf.row.cfbomcostcalcentryid;
                  this.bomCostcalcentryList = this.setTreeData(bomCostcalcentryList1);
                  let source = this.filterList(this.bomCostcalcentryList);
                  this.form.bomCostcalcentryList = this.getTreeData(source);
                  getBomCoffe(this.form).then(response => {
                    let list = response.list;
                    this.bomCostcalcentryList = this.getTreeData(this.bomCostcalcentryList);
                    if(list!=undefined&&list!=null&&list.length!=0){
                      for (let i = 0; i < list.length; i++) {
                        let listElement = list[i];
                        for (let j = 0; j < this.bomCostcalcentryList.length; j++) {
                          let bomCostcalcentryListElement = this.bomCostcalcentryList[j];
                          if(bomCostcalcentryListElement.cfbomcostcalcentryid==listElement.cfbomcostcalcentryid){
                            let row = this.materialBomDosageUpdate(listElement);
                            this.bomCostcalcentryList[j] = row;
                          }
                        }
                      }
                    }
                    this.formatBillAmount(this.bomCostcalcentryList);
                    this.entryloading = false;
                  });
                })
            }
            else{
              this.switchDataSource(this.basedatadialog.materialhalf.row);
              this.entryloading = false;
            }
          }
          else if(this.basedatadialog.materialhalf.row.cfbomcostcalcnumbe==="2"){
            if (data.specialmaterial != null) {
              let creatNewEntry1 = this.clearEntry(this.basedatadialog.materialhalf.row);
              creatNewEntry1.cfmaterialhalfnumb = data.specialmaterial.materialnumber;
              creatNewEntry1.cfmaterialhalfcode = data.specialmaterial.id;
              creatNewEntry1.cfmaterialhalfname = data.specialmaterial.materialname;
              creatNewEntry1.cfmaterialhalfunit = data.specialmaterial.baseunitname;
              creatNewEntry1.cftaxrate = data.specialmaterial.taxrate;
              creatNewEntry1.cfrawmaterialid = data.specialmaterial.group1number;
              let bomCostcalcentryList = this.bomCostcalcentryList;
              let treeData = this.getTreeData(bomCostcalcentryList);
              let list = [];
              for (let i = 0; i < treeData.length; i++) {
                if(treeData[i].cfbomcostcalcentryid == creatNewEntry1.cfbomcostcalcentryid){
                  // treeData[i] = creatNewEntry1;
                  list.push(creatNewEntry1)
                }else {
                  if(!(treeData[i].cfbomcostcalcentryid+"").includes(creatNewEntry1.cfbomcostcalcentryid+"")){
                    list.push(treeData[i])
                  }
                }
              }
              this.bomCostcalcentryList = this.setTreeDataIncludeOrdinalwidth(list)
              this.entryloading = false;
            }
            else{
              this.switchDataSource(this.basedatadialog.materialhalf.row);
              this.entryloading = false;
            }
          }
          break;
        case "specialmaterial":
          if (data.specialmaterial != null) {
            if(this.basedatadialog.specialmaterial.isEntry){
              if(this.basedatadialog.specialmaterial.isSpecial){
              }else{
                this.basedatadialog.specialmaterial.row.cfrawmaterialnumbe = data.specialmaterial.materialnumber;
                this.basedatadialog.specialmaterial.row.cfrawmaterialunit = data.specialmaterial.baseunitname;
              }
            }else{
              this.form.cfmaterialid = data.specialmaterial.materialnumber;
              this.form.cfmaterialname = data.specialmaterial.materialname;
              this.form.cfbaseunit = data.specialmaterial.baseunitname;
              this.form.cfmodel = data.specialmaterial.specifications;
              this.form.cftaxrate = data.specialmaterial.taxrate;
              this.form.fhandlerid = "1";
              this.formatBillAmount([]);
            }
          }else{
            this.form.cfmaterialid = null;
            this.removeBillMaterial();
          }
          break;
      }
    },
  }
};
</script>
